Sometimes, while working with data in form of records, we can have a problem in which we need to concatenate elements of all the records received. This is a very common application that can occur in Data Science domain. Let’s discuss certain ways in which this task can be performed.
Method #1: Using generator expression + join() This is the most basic method to achieve solution to this task. In this, we iterate over whole nested lists using generator expression and get the concatenated elements using join().
# Python3 code to demonstrate working of # Concatenate All Records # using join() + generator expression # initialize list test_list = [( 'geeksforgeeks ' , 'is' ), ( ' best' , ' for' ), ( ' all' , ' geeks' )]
# printing original list print ( "The original list : " + str (test_list))
# Concatenate All Records # using join() + generator expression res = "".join(j for i in test_list for j in i)
# printing result print ( "The Concatenated elements of list is : " + res)
|
The original list : [('geeksforgeeks ', 'is'), (' best', ' for'), (' all', ' geeks')] The Concatenated elements of list is : geeksforgeeks is best for all geeks
Time Complexity: O(n) where n is the number of tuples in the list.
Auxiliary Space: O(1) as only a few variables are used.
Method #2 : Using join() + map() + chain.from_iterable() The combination of above methods can also be used to perform this task. In this, the extension of concatenation is done by combination of map() and from_iterable().
# Python3 code to demonstrate working of # Concatenate All Records # using join() + map() + chain.from_iterable() from itertools import chain
# initialize list test_list = [( 'geeksforgeeks ' , 'is' ), ( ' best' , ' for' ), ( ' all' , ' geeks' )]
# printing original list print ( "The original list : " + str (test_list))
# Concatenate All Records # using join() + map() + chain.from_iterable() res = "".join( map ( str , chain.from_iterable(test_list)))
# printing result print ( "The Concatenated elements of list is : " + str (res))
|
The original list : [('geeksforgeeks ', 'is'), (' best', ' for'), (' all', ' geeks')] The Concatenated elements of list is : geeksforgeeks is best for all geeks
Time Complexity: O(n), where n is the total number of characters in all the tuples in the list.
Auxiliary Space: O(n), as we are using an additional string to store the concatenated result.
Method #3 : Using extend() and join() methods
# Python3 code to demonstrate working of # Concatenate All Records # initialize list test_list = [( 'geeksforgeeks ' , 'is' ), ( ' best' , ' for' ), ( ' all' , ' geeks' )]
# printing original list print ( "The original list : " + str (test_list))
# Concatenate All Records x = []
for i in test_list:
x.extend( list (i))
res = " " .join(x)
# printing result print ( "The Concatenated elements of list is : " + res)
|
The original list : [('geeksforgeeks ', 'is'), (' best', ' for'), (' all', ' geeks')] The Concatenated elements of list is : geeksforgeeks is best for all geeks
Time complexity: O(n), where n is the total number of elements in the list.
Auxiliary space: O(n), where n is the total number of elements in the list.
Method #4 : Using reduce() + join()
from functools import reduce
# initialize list test_list = [( 'geeksforgeeks ' , 'is' ), ( ' best' , ' for' ), ( ' all' , ' geeks' )]
# printing original list print ( "The original list : " + str (test_list))
# Concatenate All Records result = " " .join( reduce ( lambda x, y: x + y, test_list))
# printing result print ( "The Concatenated elements of list is : " , result)
#This code is contributed by Edula Vinay Kumar Reddy |
The original list : [('geeksforgeeks ', 'is'), (' best', ' for'), (' all', ' geeks')] The Concatenated elements of list is : geeksforgeeks is best for all geeks
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n)
Method #5: Using a loop to concatenate the elements
The program initializes a list of tuples and prints the original list. Then, it loops through each tuple and concatenates all the elements of each tuple to a single string variable called “res”. Finally, it prints the concatenated result.
# Python3 code to demonstrate working of # Concatenate All Records # using a loop # initialize list test_list = [( 'geeksforgeeks ' , 'is' ), ( ' best' , ' for' ), ( ' all' , ' geeks' )]
# printing original list print ( "The original list : " + str (test_list))
# Concatenate All Records # using a loop res = ''
for i in test_list:
for j in i:
res + = j
# printing result print ( "The Concatenated elements of list is : " + res)
|
The original list : [('geeksforgeeks ', 'is'), (' best', ' for'), (' all', ' geeks')] The Concatenated elements of list is : geeksforgeeks is best for all geeks
Time complexity: O(n^2), where n is the number of tuples in the list.
Auxiliary space: O(n^2), since the concatenated string ‘res’ is built up by appending each element of each tuple, resulting in a string of length proportional to the number of tuples and the length of each tuple.
Method #6: Using list comprehension and join()
Step-by-step approach:
- Initialize the list of tuples.
- Use a list comprehension to iterate through each tuple in the list and join its elements using the join() method.
- Join the resulting list of strings using the join() method to obtain the final concatenated string.
# Python3 code to demonstrate working of # Concatenate All Records # using list comprehension and join() # initialize list test_list = [( 'geeksforgeeks ' , 'is' ), ( ' best' , ' for' ), ( ' all' , ' geeks' )]
# printing original list print ( "The original list : " + str (test_list))
# Concatenate All Records # using list comprehension and join() res = ' '.join([' '.join(t) for t in test_list])
# printing result print ( "The Concatenated elements of list is : " + res)
|
The original list : [('geeksforgeeks ', 'is'), (' best', ' for'), (' all', ' geeks')] The Concatenated elements of list is : geeksforgeeks is best for all geeks
Time complexity: O(n), where n is the total number of characters in all the tuples.
Auxiliary space: O(n), where n is the total number of characters in all the tuples, because we are creating a new list of concatenated strings.