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 the 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 attribute to join.
# 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)) |
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’)]
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)) |
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’)]
Recommended Posts:
- Python | Convert string tuples to list tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Remove duplicate tuples from list of tuples
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Find the tuples containing the given element from a list of tuples
- Python | Count tuples occurrence in list of tuples
- Python | How to Concatenate tuples to nested tuples
- Python | Min and Max value in list of tuples
- Python | Unzip a list of tuples
- Python | Summation of tuples in list
- Python | List of tuples to String
- Python | Group tuples in list with same first value
- Python | Summation of two list of tuples
- Python | Get unique tuples from list
- Python | Get duplicate tuples from list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


