Python - Ways to iterate tuple list of lists

Lists are fundamental containers in Python programming used in day-to-day development. When dealing with nested structures like tuple lists of lists, knowing different iteration methods becomes essential for efficient data manipulation.

Using zip_longest() with List Comprehension

The zip_longest() function from itertools allows iteration across multiple lists of different lengths, filling missing values with None ?

# using itertools.zip_longest
from itertools import zip_longest

# initialising list of lists
test_list = [
    [('11'), ('12'), ('13')],
    [('21'), ('22'), ('23')],
    [('31'), ('32'), ('33')]
]

# printing initial list
print("Initial List =", test_list)

# iterate tuple list of lists 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']

Using zip_longest() with chain() and filter()

This approach combines chain() to flatten and filter() to remove None values ?

# using itertools.zip_longest + lambda + chain
from itertools import zip_longest, chain

# initialising list of lists
test_list = [
    [('11'), ('12'), ('13')],
    [('21'), ('22'), ('23')],
    [('31'), ('32'), ('33')]
]

# printing initial list
print("Initial List =", test_list)

# iterate tuple list of lists 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']

Using Simple List Comprehension

A straightforward nested list comprehension approach to flatten the structure ?

# using list comprehension
# initialising list of lists
test_list = [
    [('11'), ('12'), ('13')],
    [('21'), ('22'), ('23')],
    [('31'), ('32'), ('33')]
]

# printing initial list
print("Initial List =", test_list)

# iterate tuple list of lists into single list
# using list comprehension
res_list = [item for sublist in test_list for item in sublist]

# 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']

Comparison

Method Complexity Output Order Best For
zip_longest() Medium Column-wise Transpose-like iteration
chain() + filter() High Column-wise Functional programming style
List comprehension Low Row-wise Simple flattening

Conclusion

Use simple list comprehension for basic flattening needs. Choose zip_longest() when you need column-wise iteration across nested structures. The chain() method offers functional programming benefits but with added complexity.

Updated on: 2026-03-25T09:20:23+05:30

248 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements