Nested List Comprehension In Python (With Examples)

Sharing is caring!

Last Updated on July 24, 2022 by Jay

Sometimes we might need to use a nested list comprehension, which is equivalent to a nested loop in Python. This kind of list comprehension can be confusing sometimes, we’ll try to understand it with a few simple examples.

A Simple List Comprehension

A list comprehension is a way of writing a loop in just one line of code in Python. It’s not only fancy but also very convenient to write loops in some cases.

For Loop

Consider the following example where we use a for loop to print out each character in string “Python”:

var = "Python"
for i in var:
    print(i)
P
y
t
h
o
n

List Comprehension

The same for loop can be re-written as a list comprehension below. Note that a list comprehension returns a list.

[print(i) for i in var]
P
y
t
h
o
n
[None, None, None, None, None, None]

The below picture will help visualize how to construct a list comprehension. When in doubt, write the normal for loop first, then convert it into a list comprehension using the below three simple steps:

  1. Create an empty list [].
  2. Inside the empty list, write the action to repeat first. In our case, it’s the print(i).
  3. Write the for … line following the action.

A list comprehension always follows this pattern: write the stuff inside the loop first, followed by the for … line.

Note from the above example that the list comprehension output has an extra line [None, None, None, None, None]. This is the returned list, which contains six print() statements. A print statement returns None, hence the six None values inside a list.

A Nested List Comprehension In Python

A nested list comprehension is therefore equivalent to a nested loop. Let’s take a look at the below 2-layer nested list. To loop through it with for loop, we would do the following:

nums = [[1,2,3],[4,5,6],[7,8,9]]
for r in nums:
    for i in r:
        print(i)
1
2
3
4
5
6
7
8
9

Let’s recall how to create a list comprehension:

  1. Write the stuff inside the loop
  2. Followed by the for … line

Hence the 2-layer nested list comprehension becomes:

[print(i) for r in nums for i in r]
1
2
3
4
5
6
7
8
9
[None, None, None, None, None, None, None, None, None]

The below will help visualize the steps:

2-layer Nested List Comprehension

3-layer Nested List Comprehension

Let’s look at a more complicated example with a 3-layer nested list.

nums_2 = [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]]
## Using for loop
for r in nums_2:  
	for i in r:
		for j in i:
			print(j)
		
1
2
3
4
5
6
7
8
9
10
11
12


## Using nested list comprehension
[print(j) for r in nums_2 for i in r for j in i]
1
2
3
4
5
6
7
8
9
10
11
12
[None, None, None, None, None, None, None, None, None, None, None, None]

Again the below picture will help visualize how to construct the 3-layer nested list comprehension.

3-layer Nested List Comprehension

Additional Resources

Looping with list comprehension

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *