0
0
Pythonprogramming~10 mins

Nested lists in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested lists
Create outer list
Add inner lists as elements
Access outer list element (inner list)
Access element inside inner list
Use nested loops to iterate
Process or print inner elements
We create a list that contains other lists. We access inner lists by indexing the outer list, then access elements inside those inner lists.
Execution Sample
Python
matrix = [[1, 2], [3, 4], [5, 6]]
for i in range(len(matrix)):
    for j in range(len(matrix[i])):
        print(matrix[i][j])
This code creates a nested list (a list of lists) and prints each number inside it using nested loops.
Execution Table
Stepi (outer index)j (inner index)Condition i < len(matrix)Condition j < len(matrix[i])ActionOutput
1000 < 3 → True0 < 2 → TruePrint matrix[0][0]1
2010 < 3 → True1 < 2 → TruePrint matrix[0][1]2
3020 < 3 → True2 < 2 → FalseInner loop ends
4101 < 3 → True0 < 2 → TruePrint matrix[1][0]3
5111 < 3 → True1 < 2 → TruePrint matrix[1][1]4
6121 < 3 → True2 < 2 → FalseInner loop ends
7202 < 3 → True0 < 2 → TruePrint matrix[2][0]5
8212 < 3 → True1 < 2 → TruePrint matrix[2][1]6
9222 < 3 → True2 < 2 → FalseInner loop ends
103-3 < 3 → False-Outer loop ends
💡 Outer loop ends when i reaches 3 because 3 < 3 is False
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8Final
i-001122233
j-01010122-
Output-123456---
Key Moments - 3 Insights
Why do we need two loops to access elements in nested lists?
Because the outer list contains inner lists, the first loop picks an inner list, and the second loop goes through elements inside that inner list. See execution_table rows 1-9.
Why does the inner loop stop when j equals 2 for the first inner list?
Because the inner list has length 2, so when j reaches 2, the condition j < len(matrix[i]) becomes False (see row 3).
What happens if we try to access matrix[3][0]?
It causes an error because the outer list has only 3 elements (indices 0,1,2). The outer loop stops before i reaches 3 (see row 10).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'i' at step 7?
A0
B1
C2
D3
💡 Hint
Check the 'i (outer index)' column at step 7 in the execution_table.
At which step does the inner loop first end for the outer list's first element?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for the first row where 'Condition j < len(matrix[i])' is False in execution_table.
If the inner lists had 3 elements instead of 2, how would the execution table change?
AThe outer loop would run fewer times
BThere would be more steps with j = 2 printing new values
CThe inner loop would end earlier
DThe outer loop would never end
💡 Hint
Check how the inner loop depends on the length of inner lists in execution_table rows.
Concept Snapshot
Nested lists are lists inside lists.
Access with two indices: outer and inner.
Use nested loops to visit all elements.
Outer loop picks inner list, inner loop picks element.
Index carefully to avoid errors.
Full Transcript
Nested lists are lists that contain other lists as elements. To access elements inside, we first select an inner list by indexing the outer list, then select an element inside that inner list by indexing again. This requires two indices. To process all elements, we use nested loops: the outer loop goes through each inner list, and the inner loop goes through each element inside that inner list. The loops stop when indices reach the lengths of their respective lists. Trying to access beyond these lengths causes errors. This example code creates a nested list called matrix with three inner lists, each containing two numbers. The nested loops print each number one by one. The execution table shows each step, the values of indices, conditions checked, actions taken, and output printed. The variable tracker shows how the indices and output change over time. Key moments clarify why two loops are needed, why loops stop at certain points, and what happens if we try to access out-of-range elements. The quiz tests understanding of index values at steps, loop ending conditions, and how changing inner list size affects execution. The snapshot summarizes the concept in a few lines for quick review.