Nested loops & 2-D lists
A loop inside a loop
- You can put one loop inside another loop.
- For every pass of the outer loop, the inner loop runs fully.
- So an outer of 2 and an inner of 3 runs the body 2 × 3 = 6 times.
for row in range(2):
for col in range(3):
print(row, col)
A list of lists
- A 2-D list is a list whose items are themselves lists.
- It models a grid or table: rows of columns.
- Reach one cell with two indexes:
grid[row][col].
grid = [[1, 2], [3, 4]]
print(grid[0][1])
print(grid[1][0])
Loop over a grid
- Use a nested loop to visit every cell.
- The outer loop walks the rows; the inner loop walks the columns.
for row in grid:gives one row (a list) at a time.
grid = [[1, 2], [3, 4]]
for row in grid:
for value in row:
print(value)
In Cambridge pseudocode
- We model the exam's fixed-size 2-D array with a Python list of lists. Cambridge arrays declare bounds and an element type; Python rows can have different lengths. Read the declared bounds rather than assuming 1 in every program.
DECLARE Grid : ARRAY[1:2, 1:2] OF INTEGER
DECLARE R : INTEGER
DECLARE C : INTEGER
FOR R ← 1 TO 2
FOR C ← 1 TO 2
Grid[R, C] ← (R - 1) * 2 + C
OUTPUT Grid[R, C]
NEXT C
NEXT R
The pseudocode assigns each cell before reading it. It prints 1, 2, 3, 4, matching the Python grid above. A declaration alone is not a promise that every cell already contains zero.
Common mistakes
- The inner loop finishes all its turns for each single turn of the outer loop.
- Index a grid row first, then column:
grid[row][col]. - Repeating a mutable row repeats its reference:
row = [0, 0]; grid = [row, row]shares one row. Changinggrid[0][0]also changesgrid[1][0]. Create a fresh row on each outer-loop pass. - Iterate over the values of each Python row to handle different row lengths and empty rows. A total or count starts at zero and stays zero for an empty grid. Return the result when the algorithm is wrapped in a function.
Now you try
- Use a nested loop in each task.
- Press Check answer to test your code.
A 2-D list is a grid
A 2-D list is rows of columns; reach a cell with grid[row][col].
Difficulty 3/5 · 200 coins + a small bonus on first completion today
Use a nested loop to print a 3-by-3 square of stars: three lines, each with three * characters.
Click Run to see the output here.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
Write sum_grid(grid) that uses a nested loop and returns the sum of every number. For [[1, 2, 3], [4, 5, 6]] it returns 21. It must also work for changed values, unequal row lengths, empty rows and an empty grid.
Click Run to see the output here.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
Write count_zeros(grid) that returns the number of cells equal to zero, using nested loops. [[1, 0, 2], [0, 5, 0]] gives 3. Test another grid too; an empty grid gives 0.
Click Run to see the output here.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
Write zero_grid(rows, cols) returning a list of rows separate lists, each containing cols zeros. Both dimensions are non-negative. Changing one returned row must leave every other row unchanged. Repair the shared-row bug in the starter.
Click Run to see the output here.