Open In App

Ways to Iterate Tuple List of Lists - Python

Last Updated : 28 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article we will explore different methods to iterate through a tuple list of lists in Python and flatten the list into a single list. Basically, a tuple list of lists refers to a list where each element is a tuple containing sublists and the goal is to access all elements in a way that combines the sublists into one continuous list.

Using itertools.zip_longest

zip_longest function from itertools groups elements from each sublist of the list of lists (li) and uses None to fill missing values if the sublists which have different lengths. After that we use list comprehension to remove the None values.

Python
from itertools import zip_longest

li = [
         [('11'), ('12'), ('13')],
         [('21'), ('22'), ('23')],
         [('31'), ('32'), ('33')]
]

# Flattening list using zip_longest
res = [item for l in zip_longest(*li) for item in l if item]

print("Resultant List:", res)

Output
Resultant List: ['11', '21', '31', '12', '22', '32', '13', '23', '33']

Explanation:

  • zip_longest(*li): Groups elements of each sublist from li into tuples and filling with None if lists have unequal lengths.
  • List Comprehension is used to convert or flattens the tuples into a list skipping None values.

Using itertools.chain and zip_longest

This method chains the elements from zip_longest into a single iterable using itertools.chain and then a lambda function is applied to filter out None values before converting the result into a list.

Python
from itertools import zip_longest, chain

li = [
         [('11'), ('12'), ('13')],
         [('21'), ('22'), ('23')],
         [('31'), ('32'), ('33')]
]

# Flattening list using lambda, chain, and zip_longest
res = list(filter(lambda x: x, chain(*zip_longest(*li))))

print("Resultant List:", res)

Output
Resultant List: ['11', '21', '31', '12', '22', '32', '13', '23', '33']

Explanation:

  • zip_longest(*li) is used to group elements from sublists in li and filling with None for unequal lengths then the lambda function filters out None values.
  • chain(*zip_longest(*li)) flattens the list into a single iterable.

Using List Comprehension

List comprehension is used to flatten the list by iterating through each sublist in li and extracting each element. 

Python
li = [
         [('11'), ('12'), ('13')],
         [('21'), ('22'), ('23')],
         [('31'), ('32'), ('33')]
]

# Flattening list using list comprehension
res = [item for sublist in li for item in sublist]

print("Resultant List:", res)

Output
Resultant List: ['11', '12', '13', '21', '22', '23', '31', '32', '33']

Using sum Function

The sum function is used to flatten the list of lists by specifying an empty list [] at the start and then it merges all the sublists into a single list.
Syntax of sum function with 2 arguments is :

sum(iterable, start) where

  • iterable: The sequence (or any iterable) whose elements you want to sum.
  • start: This is an optional value that is added to the sum of the iterable and the default value is 0.
Python
li = [
    [('11'), ('12'), ('13')],
    [('21'), ('22'), ('23')],
    [('31'), ('32'), ('33')]
]

# Flattening the list using sum function
  res = sum(li, [])

print("Resultant List:", res)

Output
Resultant List: ['11', '12', '13', '21', '22', '23', '31', '32', '33']

Using map and lambda

In this method map() is used with a lambda function that simply returns each sublist and itertools.chain flattens the sublists into a single list.

Python
import itertools

li = [
    [('11'), ('12'), ('13')],
    [('21'), ('22'), ('23')],
    [('31'), ('32'), ('33')]
]

# Flattening the list using map and itertools.chain
res = list(itertools.chain(*map(lambda x: x, li)))

print("Resultant List:", res)

Output
Resultant List: ['11', '12', '13', '21', '22', '23', '31', '32', '33']

Explanation: map(lambda x: x, li) applies the lambda function to each element of li which simply returns each sublist and itertools.chain is used to flatten the list of lists by chaining all the sublists into a single sequence.


Next Article
Practice Tags :

Similar Reads