Flatten a list of tuples to a list in python

Flatten List of Tuples to a single List in Python

In this tutorial, we’ll look at how to flatten a list of tuples in Python to a single list through some examples.

Flatten a list of tuples to a list in python

There are a number of ways to flatten a list of tuples in python. You can use a list comprehension, the itertools library, or simply loop through the list of tuples adding each item to a separate list, etc. Let’s see them in action through examples followed by a runtime assessment of each.

Here, we iterate through each item of all the tuples in a nested loop and append them to a separate list.

# list of tuples
ls = [('a','b','c'),('d','e','f'),('g','h','i')]
 
# iterate through list of tuples in a nested loop
flat_ls = []
for tup in ls:
    for item in tup:
        flat_ls.append(item)

# display the lists
print("Original list:", ls)
print("Flattened list:", flat_ls)

Output:

Original list: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']] 
Flattened list: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

In the above example, the outer loop iterates over all the tuples while the inner loop iterates over each item of a tuple appending it to the list flat_ls.

You can also use a list comprehension to basically do the same thing as in the previous step but with better readability and faster execution.

# list of tuples
ls = [('a','b','c'),('d','e','f'),('g','h','i')]

# flatten using list comprehension
flat_ls = [item for tup in ls for item in tup]
 
# print
print("Original list:", ls)
print("Flattened list:", flat_ls)

Output:

Original list: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
Flattened list: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

Here, we use list comprehension to create a flattened list by keeping each item of each of the tuples in the original list. Using list comprehension is similar to nested looping but has better readability (in this use case).

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

The python itertools standard library offers handy functionalities for working with iterables. Here, we break the list of tuples into individual parts and then chain them together into a single iterable.

import itertools

# list of tuples
ls = [('a','b','c'),('d','e','f'),('g','h','i')]
 
# using itertools
flat_ls = list(itertools.chain(*ls))
 
# print
print("Original list:", ls)
print("Flattened list:", flat_ls)

Output:

Original list: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
Flattened list: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

We get the resulting flattened list with all the elements from each tuple.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

2 thoughts on “Flatten List of Tuples to a single List in Python”

  1. Nice and simple, thanks! I think it would be better if both example 1 and example 2 kept the same names for variables so one could have a better understanding of list comprehensions — like
    flat_ls = []
    for tup in ls:
    for item in tup:
    flat_ls.append(item)
    Thank you.

    1. Thank you for pointing it out.
      It definitely makes more sense to use the same variable names across the two examples as you suggest. We have updated the tutorial accordingly.

Comments are closed.

Scroll to Top