Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python Nested Comprehension: Syntax and Loop Order

How Python nested comprehensions work: loop order, filtering, dictionary and set variants, readability limits, and performance tradeoffs.

~5 min readPython
#Python#List Comprehension#Nested Loops#Code Readability
Diagram showing nested loops collapsing into a single flat list, illustrating python nested comprehension.

A Python nested comprehension is a comprehension expression that contains more than one for clause, letting you iterate over multiple sequences and produce a flat or structured result in a single expression. The syntax looks compact, but the order of the clauses determines the order of iteration, and that is where most mistakes happen.

How the Loop Order Maps to the Result

The for clauses in a nested comprehension are evaluated left to right, exactly like nested for statements. The first for clause is the outer loop, and each subsequent clause is nested inside the previous one.

python
matrix = [[1, 2, 3], [4, 5, 6]] flat = [x for row in matrix for x in row] print(flat) # [1, 2, 3, 4, 5, 6]

The equivalent explicit loop makes the order obvious:

python
flat = [] for row in matrix: for x in row: flat.append(x)

If you swap the order of the for clauses, the expression fails because row is not defined when the first clause tries to iterate over it. The leftmost clause must introduce the variable that the next clause consumes.

Building Nested Structures with Comprehensions

Nested comprehensions are not limited to flattening. You can build two-dimensional structures by nesting a comprehension inside another comprehension.

python
grid = [[0 for _ in range(4)] for _ in range(3)]

This creates a 3 by 4 grid of zeros. The outer comprehension runs three times, and each iteration evaluates the inner comprehension, producing a fresh list of four zeros. The inner comprehension must be wrapped in its own brackets; without them, the expression would produce a flat list of twelve zeros instead.

This pattern is useful for initializing matrices, game boards, or any grid where each row must be an independent list. A common mistake is to multiply a list instead:

python
bad_grid = [[0] * 4] * 3

That creates three references to the same row, so mutating one row changes all rows. A nested comprehension avoids the shared-reference problem because each inner list is constructed separately.

Filtering Inside a Nested Comprehension

An if clause can appear after a for clause and filters the items produced at that level.

python
pairs = [(x, y) for x in range(3) for y in range(3) if x != y]

Here the if follows the inner for, so it is evaluated for every (x, y) pair and keeps only pairs where the values differ. You can also place a condition after the first for clause to filter the outer loop before the inner loop runs:

python
result = [(x, y) for x in range(5) if x % 2 == 0 for y in range(3)]

Here the outer loop only visits even values of x, and the inner loop runs for each of those values. Placing the condition earlier reduces the number of inner iterations, which matters when the inner loop is expensive.

Dictionary and Set Comprehensions

The same nesting rules apply to dictionary and set comprehensions. A nested dictionary comprehension can build a mapping from structured data:

python
matrix = [[10, 20], [30, 40]] positions = {(r, c): value for r, row in enumerate(matrix) for c, value in enumerate(row)}

This comprehension keys each entry by its (row, column) tuple. Dictionary comprehensions require a key and value expression, so the nesting syntax must produce both. A nested set comprehension can collect unique values from a matrix:

python
matrix = [[1, 2, 2], [3, 3, 4]] unique_values = {x for row in matrix for x in row} print(unique_values) # {1, 2, 3, 4}

Set comprehensions behave like list comprehensions but discard duplicates. The loop-order rule is identical across all three forms.

Readability: When a Nested Comprehension Becomes a Liability

A nested comprehension with two for clauses and one condition is usually readable. Each additional clause or condition makes the expression harder to parse because the reader must track which loop a condition applies to and which variables are in scope at each level.

Consider this expression:

python
result = [transform(x, y) for x in data if x.valid for y in x.children if y.active]

The intent is understandable, but it requires careful reading. If the logic grows to three loops or multiple conditions, an explicit loop with a helper function is often clearer:

python
result = [] for x in data: if not x.valid: continue for y in x.children: if y.active: result.append(transform(x, y))

The explicit version is longer, but each condition is attached to the loop it filters, and debugging is more straightforward because you can insert logging or breakpoints at any level. A good rule of thumb is to keep a comprehension to two for clauses and at most one if; beyond that, consider a generator function or a regular loop.

Performance and Runtime Behavior

On CPython, comprehensions are typically faster than an equivalent explicit loop that appends to a list because the append operation uses optimized bytecode rather than a separate method call. That advantage remains true for nested comprehensions, but it does not change the algorithmic cost. A nested comprehension over two sequences of length n still performs n^2 iterations, and the total work is the same whether the loops are written as a comprehension or as explicit statements.

Memory behavior differs by the container you choose. A list comprehension builds the entire result in memory before returning it. For a large matrix or a Cartesian product, that can consume significant memory. A generator expression defers production of each item:

python
flat_gen = (x for row in matrix for x in row)

The generator produces one value at a time, so it avoids building the full list. If you only need to iterate once, or if the result is large, the generator form is the better choice. If you need random access or repeated iteration, a list is more practical.

Common Mistakes and Edge Cases

The most frequent error is writing the for clauses in the wrong order. The expression [x for x in row for row in matrix] fails because row is referenced before it is bound. The outer clause must always introduce the variable that the inner clause consumes.

Another mistake is forgetting that the inner comprehension in a nested structure needs its own brackets. [x for x in range(3) for _ in range(4)] produces a flat list of twelve values, while [[x for _ in range(4)] for x in range(3)] produces three lists of four values. The brackets change the shape of the result.

In Python 3, the loop variable of a comprehension is local to the comprehension and does not leak into the enclosing scope. The variable x used in [x for x in range(3)] does not overwrite an existing x in the surrounding function. This differs from explicit for loops, which do leak their loop variable, and from Python 2 list comprehensions, where the loop variable leaked. Code that relies on the old leaking behavior will behave differently, so check the Python version when moving code between environments.

Nested comprehensions are a compact tool for transforming nested data, but the compactness comes at the cost of readability when the expression grows. Keep the loop count low, place conditions where they filter the correct loop, and switch to explicit loops or generator functions when the logic no longer fits comfortably on one line.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.