Python | Ways to iterate tuple list of lists
List is an important container and used almost in every code of day-day programming as well as web-development, more it is used, more is the requirement to master it and hence knowledge of its operations is necessary.
Given a tuple list of lists, write a Python program to iterate through it and get all elements.
Method #1: Use itertools.ziplongest
# Python code to demonstrate ways to iterate # tuples list of list into single list # using itertools.ziplongest # import library from itertools import zip_longest # initialising listoflist test_list = [ [('11'), ('12'), ('13')], [('21'), ('22'), ('23')], [('31'), ('32'), ('33')] ] # printing intial list print ("Initial List = ", test_list) # iterate list tuples list of list into single list res_list = [item for my_list in zip_longest(*test_list) for item in my_list if item] # print final List print ("Resultant List = ", res_list) |
Initial List = [['11', '12', '13'], ['21', '22', '23'], ['31', '32', '33']] Resultant List = ['11', '21', '31', '12', '22', '32', '13', '23', '33']
Method#2: Using lambda + itertools.chain + zip_longest
# Python code to demonstrate # ways to iterate tuples list of list into single list # using itertools.ziplongest + lambda + chain # import library from itertools import zip_longest, chain # initialising listoflist test_list = [ [('11'), ('12'), ('13')], [('21'), ('22'), ('23')], [('31'), ('32'), ('33')] ] # printing intial list print ("Initial List = ", test_list) # iterate list tuples list of list into single list # using lambda + chain + filter res_list = list(filter(lambda x: x, chain(*zip_longest(*test_list)))) # print final List print ("Resultant List = ", res_list) |
Initial List = [['11', '12', '13'], ['21', '22', '23'], ['31', '32', '33']] Resultant List = ['11', '21', '31', '12', '22', '32', '13', '23', '33']
Method #3: Using List Comprehension
List comprehension is an elegant way to define and create list in python. We can create lists just like mathematical statements and in one line only. The syntax of list comprehension is easier to grasp.
# Python code to demonstrate ways to # iterate tuples list of list into single # list using list comprehension # initialising listoflist test_list = [ [('11'), ('12'), ('13')], [('21'), ('22'), ('23')], [('31'), ('32'), ('33')] ] # printing intial list print ("Initial List = ", test_list) # iterate list tuples list of list into single list # using list comprehension res_list = [item for list2 in test_list for item in list2] # print final List print ("Resultant List = ", res_list) |
Initial List = [['11', '12', '13'], ['21', '22', '23'], ['31', '32', '33']] Resultant List = ['11', '12', '13', '21', '22', '23', '31', '32', '33']
Recommended Posts:
- Python | Convert a list into tuple of lists
- Python | Ways to sum list of lists and return sum list
- Python | Iterate through value lists dictionary
- Python | Iterate over multiple lists simultaneously
- Iterate over a list in Python
- Python | Sort tuple list by Nth element of tuple
- Python | Unpacking tuple of lists
- Python | Sort lists in tuple
- Python | Find number of lists in a tuple
- Python | Program to count number of lists in a list of lists
- Python | Convert mixed data types tuple list to string list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Pair and combine nested list to tuple list
- Python | Merge list of tuple into list by joining the strings
- Different ways to iterate over rows in Pandas Dataframe
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.

