[20-SEC SUMMARY] Given a list of list stored in variable
- To flatten a list of lists, use the list comprehension statement
[x for l in lst for x in l].
- To modify all elements in a list of lists (e.g., increment them by one), use a list comprehension of list comprehensions
[[x+1 for x in l] for l in lst].
List comprehension is a compact way of creating lists. The simple formula is
[ expression + context ].
- Expression: What to do with each list element?
- Context: What list elements to select? It consists of an arbitrary number of for and if statements.
[x for x in range(3)] creates the list
[0, 1, 2].
In this tutorial, you’ll 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
- to iterate over a list of lists
Additionally, you’ll learn how to apply nested list comprehension. So let’s get started!
Python List Comprehension Flatten List of Lists
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.
Can you flatten a three-dimensional list (= a list of lists of lists)? Try it in the shell!
Python List Comprehension Create List of Lists
Problem: How to create a list of lists by modifying each element of an original list of lists?
Example: You’re given the list
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
You want to add one to each element and create a new list of lists:
[[2, 3, 4], [5, 6, 7], [8, 9, 10]]
Solution: Use two nested list comprehension statements, one to create the outer list of lists, and one to create the inner lists.
lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] new = [[x+1 for x in l] for l in lst] print(new) # [[2, 3, 4], [5, 6, 7], [8, 9, 10]]
Explanation: You’ll study more examples of two nested list comprehension statements later. The main idea is to use as “expression” of the outer list comprehension statement a list comprehension statement by itself. Remember, you can create any object you want in the expression part of your list comprehension statement.
Explore the code: You can play with the code in the interactive Python tutor that visualizes the execution step-by-step. Just click the “Next” button repeatedly to see what happens in each step of the code.
Let’s explore the third question: how to use list comprehension to iterate over a list of lists?
Python List Comprehension Over List of Lists
You’ve seen this in the previous example where you not only created a list of lists, you also iterated over each element in the list of lists. To summarize, you can iterate over a list of lists by using the statement
[[modify(x) for x in l] for l in lst] using any statement or function
modify(x) that returns an arbitrary object.