In this tutorial, we’ll look at how to flatten a list of tuples in Python to a single list through some examples.
How to flatten a list of tuples?
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.
1. Naive method – Iterate over the list of tuples
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
.
2. Using list comprehension
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).
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
3. Using the itertools
library to flatten list of tuples
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 –
- Python – Flatten a list of lists to a single list
- Convert Tuple to a Set in Python – With Examples
- Convert Tuple to a List in Python – With Examples
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
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.
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.