Open In App

Python | Combining tuples in list of tuples

Sometimes, we might have to perform certain problems related to tuples in which we need to segregate the tuple elements to combine with each element of complex tuple element( such as list ). This can have application in situations we need to combine values to form a whole. Let’s discuss certain ways in which this can be performed. 

Method #1: Using list comprehension We can solve this particular problem using the list comprehension technique in which we can iterate for each tuple list and join it with other tuple attributes to join. 



Step-by-step approach:

Below is the implementation of the above approach:






# Python3 code to demonstrate
# Combining tuples in list of tuples
# Using list comprehension
 
# initializing list
test_list = [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')]
 
# printing original list
print("The original list : " + str(test_list))
 
# Using list comprehension
# Combining tuples in list of tuples
res = [(tup1, tup2) for i, tup2 in test_list for tup1 in i]
 
# print result
print("The list tuple combination : " + str(res))

Output
The original list : [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')]
The list tuple combination : [(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'), (3, 'cs')]

Time Complexity: O(n^2)
Auxiliary Space: O(n)

Method #2: Using product() + list comprehension Apart from using the tuple for generation of tuples, the product function can be used to get Cartesian product of list elements with tuple element, using the iterator internally. 




# Python3 code to demonstrate
# Combining tuples in list of tuples
# Using product() + list comprehension
from itertools import product
 
# initializing list
test_list = [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')]
 
# printing original list
print("The original list : " + str(test_list))
 
# Using product() + list comprehension
# Combining tuples in list of tuples
res = [ele for i, j in test_list for ele in product(i, [j])]
 
# print result
print("The list tuple combination : " + str(res))

Output
The original list : [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')]
The list tuple combination : [(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'), (3, 'cs')]

Time complexity: O(n*m), where n is the number of tuples in the list and m is the maximum length of the tuples in the list.
Auxiliary space: O(n*m), as we are creating a new list of tuples with all possible combinations of the original tuples.

Method #3: Using enumerate function




test_list = [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')]
 
 
res = [(tup1, tup2) for i, tup2 in test_list for j, tup1 in enumerate(i)]
print(res)

Output
[(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'), (3, 'cs')]

Time complexity: O(n^2) where n is the length of the longest list inside the tuples in test_list. 
Auxiliary space: O(n^2) where n is the length of the longest list inside the tuples in test_list.

Method #4: Using for loop




# Python3 code to demonstrate
# Combining tuples in list of tuples
 
# initializing list
test_list = [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')]
 
# printing original list
print("The original list : " + str(test_list))
 
# Combining tuples in list of tuples
res = []
for i in test_list:
    a = []
    for j in i[0]:
        a.append((j, i[1]))
    res.extend(a)
 
# print result
print("The list tuple combination : " + str(res))

Output
The original list : [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')]
The list tuple combination : [(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'), (3, 'cs')]

Time Complexity: O(M*N)
Auxiliary Space: O(1)

Method #5 : Using chain.from_iterable() from the itertools module:




# import the chain function from the itertools module
from itertools import chain
 
# initialize the list of tuples
test_list = [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')]
 
# print the original list
print("The original list : " + str(test_list))
 
# flatten the list of tuples using chain.from_iterable
# the inner generator expression generates a list of tuples for each inner list in the outer list
# each inner list is paired with the second element of the tuple, and the resulting list of tuples is flattened
res = list(chain.from_iterable(((y, x[1]) for y in x[0]) for x in test_list))
 
# print the resulting list of tuples
print("The list tuple combination : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy

Output
The original list : [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')]
The list tuple combination : [(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'), (3, 'cs')]

Time complexity: O(n) where n is number of elements in all tuples because it iterates through the elements in the list of tuples once. 
Auxiliary space: O(n) because it creates a new list to store the resulting tuples.

Method #6: Using reduce() function from functools module  +  list comprehension.




# Python3 code to demonstrate
# Combining tuples in list of tuples
from functools import reduce
 
# initializing list
test_list = [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')]
 
# printing original list
print("The original list : " + str(test_list))
 
# Using reduce() + list comprehension
# Combining tuples in list of tuples
res = reduce(lambda x, y: x + [(i, y[1]) for i in y[0]], test_list, [])
 
# print result
print("The list tuple combination : " + str(res))

Output
The original list : [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')]
The list tuple combination : [(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'), (3, 'cs')]

Time complexity: O(n * m), where n is the length of the input list and m is the maximum length of a tuple in the input list.
Auxiliary space: O(n * m), where n is the length of the input list and m is the maximum length of a tuple in the input list.

Method #7: Using nested loops




# Python3 code to demonstrate
# Combining tuples in list of tuples
# Using nested loops
 
# initializing list
test_list = [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')]
 
# printing original list
print("The original list : " + str(test_list))
 
# Using nested loops
# Combining tuples in list of tuples
res = []
for i, tup2 in test_list:
    for tup1 in i:
        res.append((tup1, tup2))
 
# print result
print("The list tuple combination : " + str(res))

Output
The original list : [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')]
The list tuple combination : [(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'), (3, 'cs')]

Time complexity: O(n * m), where n is the length of the input list and m is the maximum length of a tuple in the input list.
Auxiliary space: O(n * m), where n is the length of the input list and m is the maximum length of a tuple in the input list.


Article Tags :