Python | Pair and combine nested list to tuple list
Sometimes we need to convert between the data types, primarily due to the reason of feeding them to some function or output. This article solves a very particular problem of pairing like indices in list of lists and then construction of list of tuples of those pairs. Let’s discuss how to achieve the solution of this problem.
Method #1 : Using zip() + list comprehension
We can use zip function to link to each element’s like indices and list comprehension can be used to perform the logic behind the conversion to tuple and extending the logic to all the sublists.
# Python3 code to demonstrate # Pairing and combining nested list to tuple list # using zip() + list comprehension # initializing lists test_list1 = [[1, 4, 5], [8, 7], [2]] test_list2 = [['g', 'f', 'g'], ['f', 'r'], ['u']] # printing original lists print("The original list 1 : " + str(test_list1)) print("The original list 2 : " + str(test_list2)) # using zip() + list comprehension # Pairing and combining nested list to tuple list res = [(u, v) for x, y in zip(test_list1, test_list2) for u, v in zip(x, y)] # print result print("The paired list of tuple is : " + str(res)) |
The original list 1 : [[1, 4, 5], [8, 7], [2]]
The original list 2 : [[‘g’, ‘f’, ‘g’], [‘f’, ‘r’], [‘u’]]
The paired list of tuple is : [(1, ‘g’), (4, ‘f’), (5, ‘g’), (8, ‘f’), (7, ‘r’), (2, ‘u’)]
Method #2 : Using zip() + itertools.chain.from_iterable()
This problem can also be solved using the zip function along with the from_iterable function. The task performed by zip function remains the same but conversion to tuple and do it for all elements can be handled by from_iterable function.
# Python3 code to demonstrate # Pairing and combining nested list to tuple list # using zip() + itertools.chain.from_iterable() from itertools import chain # initializing lists test_list1 = [[1, 4, 5], [8, 7], [2]] test_list2 = [['g', 'f', 'g'], ['f', 'r'], ['u']] # printing original lists print("The original list 1 : " + str(test_list1)) print("The original list 2 : " + str(test_list2)) # using zip() + itertools.chain.from_iterable() # Pairing and combining nested list to tuple list res = list(zip(chain.from_iterable(test_list1), chain.from_iterable(test_list2))) # print result print("The paired list of tuple is : " + str(res)) |
The original list 1 : [[1, 4, 5], [8, 7], [2]]
The original list 2 : [[‘g’, ‘f’, ‘g’], [‘f’, ‘r’], [‘u’]]
The paired list of tuple is : [(1, ‘g’), (4, ‘f’), (5, ‘g’), (8, ‘f’), (7, ‘r’), (2, ‘u’)]
Recommended Posts:
- Python | Check if a nested list is a subset of another nested list
- Python | Find maximum length sub-list in a nested list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Convert a nested list into a flat list
- Python | Merge list of tuple into list by joining the strings
- Python | Convert list to indexed tuple list
- Python | Convert Integral list to tuple list
- Python | Convert given list into nested list
- Python | Combine two lists by maintaining duplicates in first list
- Python | Sort tuple list by Nth element of tuple
- Python | Test for nested list
- Python | Intersection of two nested list
- Python | How to copy a nested list
- Nested List Comprehensions in Python
- Python | Mean of tuple 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.



