Open In App

Python | Records List Concatenation

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python records, we can have a problem in which we need to perform cross concatenation of list of tuples. This kind of application is popular in web development domain. Let’s discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + zip() The combination of above functionalities can be used to perform this particular task. In this, we iterate through the list using list comprehension and the concatenation across lists is performed with help of zip(). 

Python3




# Python3 code to demonstrate working of
# Records List Concatenation
# using list comprehension + zip()
 
# initialize lists
test_list1 = [("g", "f"), ("g", "is"), ("be", "st")]
test_list2 = [("fg", "g"), ("gf", "best"), ("st", " gfg")]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Records List Concatenation
# using list comprehension + zip()
res = [(x[0] + y[0], x[1] + y[1]) for x, y in zip(test_list1, test_list2)]
 
# printing result
print("The Concatenation across lists is : " + str(res))


Output : 

The original list 1 : [('g', 'f'), ('g', 'is'), ('be', 'st')]
The original list 2 : [('fg', 'g'), ('gf', 'best'), ('st', ' gfg')]
The Concatenation across lists is : [('gfg', 'fg'), ('ggf', 'isbest'), ('best', 'st gfg')]

The time complexity of the given code is O(n), where n is the size of the input lists.

The auxiliary space complexity of the given code is also O(n), as the code creates a new list of size n to store the concatenated records.

  Method #2 : Using join() + zip() + map() This is yet another way to perform this task. This is similar to above method, the difference is that summation is performed by inbuilt function and extending logic to each element is done by map(). 

Python3




# Python3 code to demonstrate working of
# Records List Concatenation
# using join() + zip() + map()
 
# initialize lists
test_list1 = [("g", "f"), ("g", "is"), ("be", "st")]
test_list2 = [("fg", "g"), ("gf", "best"), ("st", " gfg")]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Records List Concatenation
# using join() + zip() + map()
res = [tuple(map("".join, zip(a, b))) for a, b in zip(test_list1, test_list2)]
 
# printing result
print("The Concatenation across lists is : " + str(res))


Output : 

The original list 1 : [('g', 'f'), ('g', 'is'), ('be', 'st')]
The original list 2 : [('fg', 'g'), ('gf', 'best'), ('st', ' gfg')]
The Concatenation across lists is : [('gfg', 'fg'), ('ggf', 'isbest'), ('best', 'st gfg')]

Time complexity: O(n), where n is the length of the shorter list between test_list1 and test_list2.

Auxiliary space complexity: O(n), where n is the length of the shorter list between test_list1 and test_list2.

Method #3 : Using for loop

Python3




# Python3 code to demonstrate working of
# Records List Concatenation
 
# initialize lists
test_list1 = [("g", "f"), ("g", "is"), ("be", "st")]
test_list2 = [("fg", "g"), ("gf", "best"), ("st", " gfg")]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Records List Concatenation
res=[]
for i in range(0,len(test_list1)):
    a=test_list1[i][0]+test_list2[i][0]
    b=test_list1[i][1]+test_list2[i][1]
    res.append((a,b))
# printing result
print("The Concatenation across lists is : " + str(res))


Output

The original list 1 : [('g', 'f'), ('g', 'is'), ('be', 'st')]
The original list 2 : [('fg', 'g'), ('gf', 'best'), ('st', ' gfg')]
The Concatenation across lists is : [('gfg', 'fg'), ('ggf', 'isbest'), ('best', 'st gfg')]

Time Complexity : O(N)

Auxiliary Space : O(N)

METHOD 4: Using map and lambda function

APPROACH:

This code takes two lists of tuples as input and concatenates the tuples from the same index position in each list. It uses the map function along with a lambda function that takes two tuples as input and concatenates them using string concatenation (+). The resulting list of tuples is then printed.

ALGORITHM:

1.Initialize two lists, list1 and list2.
2.Use the map() function with a lambda function that concatenates the 0th and 1st elements of each tuple in list1 and list2.
3.The result is a new list with tuples containing concatenated values from list1 and list2.

Python3




list1 = [('g', 'f'), ('g', 'is'), ('be', 'st')]
list2 = [('fg', 'g'), ('gf', 'best'), ('st', ' gfg')]
 
result = list(map(lambda x, y: (x[0]+y[0], x[1]+y[1]), list1, list2))
print(result)


Output

[('gfg', 'fg'), ('ggf', 'isbest'), ('best', 'st gfg')]

Time complexity: O(n), where n is the length of the lists.
Space complexity: O(n).



Last Updated : 13 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads