Python | Concatenate two lists element-wise
Last Updated :
20 Apr, 2023
Sometimes we come across this type of problem in which we require to leave each element of one list with the other. This type of problems usually occurs in developments in which we have the combined information, like names and surnames in different lists. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + zip() List comprehension does the task of concatenating the similar index elements. The task of zip function is concatenating the resultant string into a single list and return list.
Python3
test_list1 = ["Geeksfor", "i", "be"]
test_list2 = [ 'Geeks' , 's' , 'st' ]
print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
res = [i + j for i, j in zip (test_list1, test_list2)]
print ("The list after element concatenation is : " + str (res))
|
Output:
The original list 1 is : ['Geeksfor', 'i', 'be']
The original list 2 is : ['Geeks', 's', 'st']
The list after element concatenation is : ['GeeksforGeeks', 'is', 'best']
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2 : Using map() + lambda + zip() The task of mapping each index element with each other is performed by map function in this method and the functionality of addition is performed by lambda function. This method works only in Python2.
Python
test_list1 = ["Geeksfor", "i", "be"]
test_list2 = [ 'Geeks' , 's' , 'st' ]
print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
res = list ( map ( lambda (i, j): i + j, zip (test_list1, test_list2)))
print ("The list after element concatenation is : " + str (res))
|
Output:
The original list 1 is : ['Geeksfor', 'i', 'be']
The original list 2 is : ['Geeks', 's', 'st']
The list after element concatenation is : ['GeeksforGeeks', 'is', 'best']
Method #3 : Using reduce():
This code uses the reduce() function from the functools module to concatenate the elements of two lists list1 and list2. The zip() function is used to pair the elements of the two lists together, and the lambda function passed to reduce() combines each pair of elements using string concatenation. The reduce() function returns a single list containing the concatenated elements.
Time complexity: O(n) (To concatenate all elements in worst case)
Auxiliary space: O(n) (To store the concatenated elements in a new list)
Python3
list1 = [ "Hello" , "Hi" , "Good morning" ]
list2 = [ "world" , "there" , "all" ]
from functools import reduce
result = reduce ( lambda res, l: res + [l[ 0 ] + " " + l[ 1 ]], zip (list1, list2), [])
print (result)
|
Output
['Hello world', 'Hi there', 'Good morning all']
Method #4: Using for loop:
Python3
test_list1 = [ "Geeksfor" , "i" , "be" ]
test_list2 = [ 'Geeks' , 's' , 'st' ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = []
for i in range ( len (test_list1)):
res.append(test_list1[i] + test_list2[i])
print ( "The list after element concatenation is : " + str (res))
|
Output
The original list 1 is : ['Geeksfor', 'i', 'be']
The original list 2 is : ['Geeks', 's', 'st']
The list after element concatenation is : ['GeeksforGeeks', 'is', 'best']
Time complexity: O(n)
Auxiliary Space: O(n)
Method#5:Using itertools
Python3
import itertools
test_list1 = [ "Geeksfor" , "i" , "be" ]
test_list2 = [ 'Geeks' , 's' , 'st' ]
print ( "The original list 1 is:" , test_list1)
print ( "The original list 2 is:" , test_list2)
result = list ( map (''.join, itertools.zip_longest(test_list1, test_list2)))
print ( "The list after element concatenation is:" , result)
|
Output
The original list 1 is: ['Geeksfor', 'i', 'be']
The original list 2 is: ['Geeks', 's', 'st']
The list after element concatenation is: ['GeeksforGeeks', 'is', 'best']
Time complexity: O(n)
Auxiliary Space: O(n)
Please Login to comment...