How to Write Nested List Comprehensions in Python

A nested list comprehension in Python is a list comprehension placed inside another list comprehension. It's often used for handling lists of lists with only one line of code. Here’s how to write a nested list comprehension in Python.

Written by Peter Grant
nested list comprehension - a bird's nest in a tree branch
Image: Shutterstock / Built In
Brand Studio Logo
UPDATED BY
Brennan Whitfield | Apr 11, 2025

Nested list comprehension in Python refers to creating a list comprehension inside another list comprehension. It can be used to collapse multiple lines of code for list comprehensions into one line of code, which removes unnecessary code and improves readability.

What Are Nested List Comprehensions?

List comprehensions create a new list by scanning all items in a list, checking to see if they meet a condition, passing the values that do and applying an expression to them. You can place one list comprehension within another list comprehension to create a nested list comprehension and further simplify your code.

A nested list comprehension doubles down on the concept of list comprehension — it’s a way to combine not only one, but multiple for loops, if statements and expressions into a single code line. This becomes useful when you have a list of lists (instead of only a single list).

As a matter of fact, there’s no limit to the number of comprehensions you can nest within each other in Python, which makes it possible to write very complex code in a single line.

Learning to make code easy for other people to understand is a critical task in the modern workplace. List comprehensions and nested list comprehensions are a useful Python tool to help your collaborators quickly understand how your code works.

Since nesting list comprehension can be a powerful technique, let’s take a deeper look.

 

What Is a List Comprehension?

A list comprehension is a way to create a new list by filtering elements in a list that meet a condition, transforming the elements as desired and storing the new values in a list. All we need is a single line of code. 

The code is as follows:

List Comprehension = [Expression for Value in DataSet if Condition]

That code sorts through each value in your data set, checks to see if it meets the condition, applies the stated expression and stores it in a list named Result. It applies a for loop, if statement and an expression all in a single line.

Here’s an example showing how list comprehensions work:

Numbers = [1,2,3,4,5,6,7,8,9,10]

Result_ForLoop = []
for Number in Numbers:
   if Number > sum(Numbers)/len(Numbers):
       Result_ForLoop.append(Number)

Result_ListComprehension = []
Result_ListComprehension = [Number for Number in Numbers if Number > sum(Numbers)/len(Numbers)]

print(Result_ForLoop)
print(Result_ListComprehension)

#Output:
[6, 7, 8, 9, 10]
[6, 7, 8, 9, 10]

Those two print statements will show the exact same result: [6, 7, 8, 9, 10].

Now let’s dive into nested list comprehensions.

 

How to Create a Nested List Comprehension in Python

Let’s consider a nested list comprehension example. Say you have two lists of numbers and you want to return the square of all of the even numbers. You could write the following code using a single list comprehension like so:

Code With One List Comprehension

Numbers = [[1,2,3,4,5,6,7,8,9,10],
          [11,12,13,14,15,16,17,18,19,20]]

Result = []
for list in Numbers:
   Squares = [Number ** 2 for Number in list if Number % 2 == 0]
   Result.extend(Squares)

print(Result)

#Output: [4, 16, 36, 64, 100, 144, 196, 256, 324, 400]

The above code:

  • Creates the input data set and stores it in Numbers. The input data consists of two lists. The first list runs from one to 10, and the second list runs from 11 to 20.
  • Then the code creates an empty list called Result. We will use Result to store the output from our expression.
  • The code will then create a for loop and iterate through each list in Number.
  • Within the for loop it uses a list comprehension to search through the provided list, check if each number in the list is divisible by two, squares the result and stores it in a list. This list is then stored in the variable Squares.
  • The final line then adds Squares to Result giving us a list of the squares of the values that are divisible by two.

Since there are two lists in Numbers it executes the for loop twice.

Now, since you’re familiar with list comprehensions you probably understand it’s fully possible to remove the for loop and if statement with a list comprehension. Doing so creates a nested list comprehension.

To do this we write a single list comprehension with two for loops. It’s key to remember three things when doing this:

  • The expression is always the first term.
  • The for loops are always written in the order of the nesting.
  • The if condition (if applicable) is always placed at the end.

So, to construct a nested for loop in our example we need to:

  • Write the expression to square the numbers that have passed the filter.
  • Write a for loop iterating through all of the lists in Numbers.
  • Write a for loop iterating through each number in the passed list.
  • Write a condition passing the numbers that are even.

And we need to do it in that order. Fortunately, once we’ve created that structure in our minds it’s simple to execute. 

Code With Nested List Comprehension

The code to create a nested list comprehension from the code above appears as follows:

Numbers = [[1,2,3,4,5,6,7,8,9,10],
          [11,12,13,14,15,16,17,18,19,20]]

Result = [Number ** 2 for list in Numbers for Number in list if Number % 2 == 0]

print(Result)

#Output: [4, 16, 36, 64, 100, 144, 196, 256, 324, 400]

If you replace the previous code you’ll find this nested list comprehension returns the same result.

This is a powerful way to reduce several lines of code into a single line and can significantly increase the readability of your code. Since the code is a single line, anybody who understands list comprehensions will be able to quickly deduce what is happening and follow your logic.

 

When to Use a Nested List Comprehension in Python

While nested list comprehensions are a useful way to improve your code’s readability, there is a downside. List comprehensions can get very complex very quickly. What if you have a hundred for loops? Or what if you have very complex expression and condition descriptions?

I could envision a complex nested for loop spanning several lines of code, and becoming nearly impossible to read or understand. While Python has no technical limit to understanding complex nested for loops, there is a human limit.

If you have a complex piece of code with many for loops, or complex expressions and conditions, using a nested for loop may actually make your code more difficult to understand. Take care to ensure your efforts are making your collaborators’ lives easier instead of harder.

Generally speaking, if you have more than three levels of nesting it may be easier for everybody if you just write out the for loops.

Frequently Asked Questions

A nested list in Python refers to one or more lists inside another list. To write a nested list in Python, treat each element in a list as its own list, for example:

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 

A list comprehension in Python refers to creating a new list by filtering the elements based on a specified condition, often using one line of code. 

To write a list comprehension in Python, create a list that includes an expression, element item and iterable, for example:

normal_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
squares = [i ** 2 for i in normal_list]

Lists comprehensions can also include an if condition for more control, for example:

normal_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
even_squares = [i ** 2 for i in normal_list if i % 2 == 0]

To combine two list comprehensions (known as nested list comprehension) in Python, use a list comprehension with a double for loop. This applies the nested list comprehension’s expression to all elements within elements of a list. 

For example:

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
squares = [i ** 2 for list in nested_list for i in list]

This can also be written with an if condition, for example:

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
even_squares = [i ** 2 for list in nested_list for i in list if i % 2 == 0]

The 4 types of comprehension in Python are:

  1. List comprehension
  2. Dictionary comprehension 
  3. Set comprehension
  4. Generator comprehension
Explore Job Matches.