Python | Concatenate All Records
Last Updated :
16 May, 2023
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
test_list = [( 'geeksforgeeks ' , 'is' ), ( ' best' , ' for' ), ( ' all' , ' geeks' )]
print ( "The original list : " + str (test_list))
res = "".join(j for i in test_list for j in i)
print ( "The Concatenated elements of list is : " + res)
|
Output
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
from itertools import chain
test_list = [( 'geeksforgeeks ' , 'is' ), ( ' best' , ' for' ), ( ' all' , ' geeks' )]
print ( "The original list : " + str (test_list))
res = "".join( map ( str , chain.from_iterable(test_list)))
print ( "The Concatenated elements of list is : " + str (res))
|
Output
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
test_list = [( 'geeksforgeeks ' , 'is' ), ( ' best' , ' for' ), ( ' all' , ' geeks' )]
print ( "The original list : " + str (test_list))
x = []
for i in test_list:
x.extend( list (i))
res = " " .join(x)
print ( "The Concatenated elements of list is : " + res)
|
Output
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()
Python3
from functools import reduce
test_list = [( 'geeksforgeeks ' , 'is' ), ( ' best' , ' for' ), ( ' all' , ' geeks' )]
print ( "The original list : " + str (test_list))
result = " " .join( reduce ( lambda x, y: x + y, test_list))
print ( "The Concatenated elements of list is : " , result)
|
Output
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
test_list = [( 'geeksforgeeks ' , 'is' ), ( ' best' , ' for' ), ( ' all' , ' geeks' )]
print ( "The original list : " + str (test_list))
res = ''
for i in test_list:
for j in i:
res + = j
print ( "The Concatenated elements of list is : " + res)
|
Output
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
test_list = [( 'geeksforgeeks ' , 'is' ), ( ' best' , ' for' ), ( ' all' , ' geeks' )]
print ( "The original list : " + str (test_list))
res = ' '.join([' '.join(t) for t in test_list])
print ( "The Concatenated elements of list is : " + res)
|
Output
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.
Please Login to comment...