Short answer: To flatten your list of list
lst, use the nested list comprehension
[x for l in lst for x in l] to iterate over all nested lists (
for l in lst) and over all their elements (
for x in l). Each element
x, you just place in the outer list, unchanged.
When browsing StackOverflow, I stumble upon this question from time to time: “How to Join a List of Lists?“. My first thought is that the asking person is referring to the join() function that converts an iterable (such as a list) to a string by concatenating its elements.
But nothing can be further from the truth! The question is usually about “flattening” the list: to transform a nested list of lists such as
[[1, 2, 3], [4, 5, 6]] to a flat list
[1, 2, 3, 4, 5, 6]. So, the real question is:
How to Flatten a List of Lists in Python?
Problem: Given a list of lists. How to flatten the list of lists by getting rid of the inner lists—and keeping their elements?
Example: You want to transform a given list into a flat list like here:
lst = [[2, 2], , [1, 2, 3, 4], [1, 2, 3]] # ... Flatten the list here ... print(lst) # [2, 2, 4, 1, 2, 3, 4, 1, 2, 3]
Solution: Use a nested list comprehension statement
[x for l in lst for x in l] to flatten the list.
lst = [[2, 2], , [1, 2, 3, 4], [1, 2, 3]] # ... Flatten the list here ... lst = [x for l in lst for x in l] print(lst) # [2, 2, 4, 1, 2, 3, 4, 1, 2, 3]
Explanation: In the nested list comprehension statement
[x for l in lst for x in l], you first iterate over all lists in the list of lists (
for l in lst). Then, you iterate over all elements in the current list (
for x in l). This element, you just place in the outer list, unchanged, by using it in the “expression” part of the list comprehension statement
[x for l in lst for x in l].
Try It Yourself: You can execute this code snippet yourself in our interactive Python shell. Just click “Run” and test the output of this code.
Exercise: How to flatten a three-dimensional list (= a list of lists of lists)? Try it in the shell!
Video: List Comprehension Python List of Lists
Watch the video to learn three ways how to apply list comprehension to a list of lists:
- to flatten a list of lists,
- to create a list of lists, and
- to iterate over a list of lists.
Well, if you really want to learn how to
join() a list in Python, visit this detailed tutorial on the Finxter blog.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.