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!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become 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.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.