0
0
Pythonprogramming~10 mins

Nested list comprehension in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested list comprehension
Start outer loop
Take one item from outer iterable
Start inner loop
Take one item from inner iterable
Apply expression using outer and inner items
Add result to inner list
Inner loop ends?
NoRepeat inner loop
Yes
Add inner list to outer list
Outer loop ends?
NoRepeat outer loop
Yes
Return full nested list
Nested list comprehension runs an inner loop inside an outer loop, building a list of lists by applying an expression to each pair of outer and inner items.
Execution Sample
Python
matrix = [[i * j for j in range(3)] for i in range(4)]
print(matrix)
Creates a 4x3 matrix where each element is the product of its row and column indices.
Execution Table
StepOuter iInner jExpression i*jInner list so farOuter list so far
1000*0=0[0][]
2010*1=0[0, 0][]
3020*2=0[0, 0, 0][]
40 inner loop ends[0, 0, 0][[0, 0, 0]]
5101*0=0[0][[0, 0, 0]]
6111*1=1[0, 1][[0, 0, 0]]
7121*2=2[0, 1, 2][[0, 0, 0]]
81 inner loop ends[0, 1, 2][[0, 0, 0], [0, 1, 2]]
9202*0=0[0][[0, 0, 0], [0, 1, 2]]
10212*1=2[0, 2][[0, 0, 0], [0, 1, 2]]
11222*2=4[0, 2, 4][[0, 0, 0], [0, 1, 2]]
122 inner loop ends[0, 2, 4][[0, 0, 0], [0, 1, 2], [0, 2, 4]]
13303*0=0[0][[0, 0, 0], [0, 1, 2], [0, 2, 4]]
14313*1=3[0, 3][[0, 0, 0], [0, 1, 2], [0, 2, 4]]
15323*2=6[0, 3, 6][[0, 0, 0], [0, 1, 2], [0, 2, 4]]
163 inner loop ends[0, 3, 6][[0, 0, 0], [0, 1, 2], [0, 2, 4], [0, 3, 6]]
17Outer loop ends[[0, 0, 0], [0, 1, 2], [0, 2, 4], [0, 3, 6]]
💡 Outer loop i reaches 4, condition i in range(4) is False, so comprehension ends.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
iNone0123End
jNone012EndEnd
Inner list[][0, 0, 0][0, 1, 2][0, 2, 4][0, 3, 6]Final inner list cleared after each outer iteration
Outer list[][[0, 0, 0]][[0, 0, 0], [0, 1, 2]][[0, 0, 0], [0, 1, 2], [0, 2, 4]][[0, 0, 0], [0, 1, 2], [0, 2, 4], [0, 3, 6]]Final nested list
Key Moments - 3 Insights
Why does the inner list get reset for each outer loop iteration?
Because the inner list comprehension runs fresh for each outer i, building a new list before adding it to the outer list. See execution_table rows 4, 8, 12, and 16 where inner loop ends and inner list is added.
What does the expression i*j represent in this nested comprehension?
It calculates the product of the current outer loop value i and inner loop value j, producing each element in the inner list. See execution_table column 'Expression i*j' for each step.
Why is the final result a list of lists?
Because the outer list comprehension collects each inner list (from the inner comprehension) into one big list, creating a nested list structure. See execution_table column 'Outer list so far' growing after each inner loop ends.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the inner list after step 7?
A[0, 2, 4]
B[0, 0, 0]
C[0, 1, 2]
D[0, 3, 6]
💡 Hint
Check the 'Inner list so far' column at step 7 in the execution_table.
At which step does the outer list first contain two inner lists?
AStep 4
BStep 8
CStep 12
DStep 16
💡 Hint
Look at the 'Outer list so far' column and find when it has two inner lists.
If the inner range changed from range(3) to range(2), how would the inner list length change?
AIt would have 2 elements
BIt would have 3 elements
CIt would have 4 elements
DIt would be empty
💡 Hint
The inner loop runs for each j in range(3) originally; changing to range(2) reduces inner iterations to 2.
Concept Snapshot
Nested list comprehension syntax:
[[expression for inner_var in inner_iterable] for outer_var in outer_iterable]

Runs inner loop fully for each outer loop item.
Builds a list of lists.
Useful for creating matrices or grids.
Each inner list is independent and rebuilt each outer iteration.
Full Transcript
Nested list comprehension in Python means putting one list comprehension inside another. The outer loop picks each item, then the inner loop runs fully for that item, creating a list. This inner list is added to the outer list. The example multiplies outer and inner indices to build a 4 by 3 matrix. Step by step, the inner list builds up, then is added to the outer list. This repeats until the outer loop finishes. The final result is a list of lists, like a grid or matrix. Beginners often wonder why the inner list resets each time; it's because the inner comprehension runs fresh for each outer item. The expression uses both loop variables to create each element. Changing the inner loop range changes the size of each inner list. This concept is useful for creating tables, grids, or any nested data structure in a compact way.