← VCE Software Development U3O1

Traversing a 2D array with nested loops

Reads one element of a 2D array. An outer loop steps through each row and an inner loop steps through each column, so every element in the grid gets visited.

Understanding Traversing a 2D array with nested loops in VCE Software Development U3O1

What it does: The inner loop prints all items in one row on the same line using end=' ', and the bare print() after it starts a new line for the next row. Nested loops visit every element of the grid, and the if statement compares each element grid[r][c] with the target value being searched for. The outer loop picks the row and the inner loop picks each column in that row. Without an inner loop, col is never defined and only one position per row would be read. Resetting total to 0 for each row stops one row's total carrying into the next. If it is set only once at the start, the totals keep adding up across all rows.

Common mistakes: The grid has only 2 rows, so the outer loop must use range(2). range(3) makes r reach 2, and grid[2] causes an IndexError. It should be: for r in range(2):. The row total must start at 0 for every row. Starting at 1 makes every row total one too high. It should be: total = 0.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 72–96) and word search (Modules 72–113).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which nested loop visits every element of a 2D list called grid?
for row in range(len(grid)): print(grid[row][col])for row in range(len(grid)): for col in range(len(grid[row])): print(grid[row][col])
Matches
for row in range(3): for col in range(4): print(grid[col][row])
↓
The indexes are swapped. grid[col][row] uses the column number as the row index, so the wrong cells are read or an index error occurs. It should be grid[row][col].
Applying
Meanings
Term
grid[row][col]
Meaning
Reads one element of a 2D array. An outer loop steps through each row and an inner loop steps through each column, so every element in the grid gets visited.
Practice
grid = [[3, 5, 2], [4, 1, 6], [7, 8, 9]]
Testing
Gaps
for row in range((grid)):
Check Answer
Spots
1grid = [[1, 2, 3], [4, 5, 6]]
2for r in range(3):

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Hermit Crab' badge — Definition Collector, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which nested loop visits every element of a 2D list called grid?
A. for row in range(len(grid)): for col in range(len(grid[row])): print(grid[row][col]) ✓
B. for row in range(len(grid)): print(grid[row][col])
The outer loop picks the row and the inner loop picks each column in that row. Without an inner loop, col is never defined and only one position per row would be read.
2. In grid[row][col], which index picks the row?
A. The second index, col
B. The first index, row ✓
The first index selects the row (the inner list). The second index selects the column inside that row.
3. Which loop structure makes the inner loop run completely for each step of the outer loop?
A. The inner loop is written after the outer loop at the same indentation
B. The inner loop is indented inside the outer loop ✓
Indenting the inner loop inside the outer loop makes it restart and run fully for every row. At the same indentation, the two loops would just run one after the other.
4. A grid has 3 rows and 4 columns. How many times does the inner loop body run in total?
A. 12 times (3 x 4) ✓
B. 7 times (3 + 4)
The inner loop runs 4 times for each of the 3 rows, so the body runs 3 x 4 = 12 times.
5. Where should you set total = 0 to calculate a separate total for each row?
A. Inside the outer loop, before the inner loop starts ✓
B. Before the outer loop starts, once only
Resetting total to 0 for each row stops one row's total carrying into the next. If it is set only once at the start, the totals keep adding up across all rows.

Match the Pairs

for row in range(len(grid)): for col in range(len(grid[row])): → Outer loop steps through each row and the inner loop steps through each column in that row, so every element is visited
total += grid[row][col] → Adds the current element to a running total while the nested loops visit each cell
Print a grid row by row: the inner loop builds one row, then the outer loop moves to the next line → for row in grid: for item in row: print(item, end=' ') print()
Find a value in a grid by checking every element with nested loops → for r in range(len(grid)): for c in range(len(grid[r])): if grid[r][c] == target:
for row in range(3): for col in range(4): print(grid[col][row]) → The indexes are swapped. grid[col][row] uses the column number as the row index, so the wrong cells are read or an index error occurs. It should be grid[row][col].
for row in range(len(grid)): total = 0 for col in range(len(grid[row])): total += grid[row][col] total_all = total → total is reset to 0 inside the outer loop and never saved, so each row total is lost and only the last row remains. Print or store each row total after the inner loop, before the next row begins.

Key Term

grid[row][col]
Reads one element of a 2D array. An outer loop steps through each row and an inner loop steps through each column, so every element in the grid gets visited.

Fill in the Blanks

1. for row in range(len(grid)): for col in range(len(grid[row])): print(grid[row][col])
Hint: Type the function that counts how many rows the grid has.
2. for row in range(3): for col in range(3): totals[col] += grid[row][col]
Hint: Type the outer loop variable that picks which row to read from.
3. for row in range(len(grid)): for col in range(len(grid[row])): print(grid[row][col], end=' ')
Hint: Type the keyword argument name that stops print() starting a new line after each element.
4. for row in range(len(grid)): for col in range(len(grid[row])): if grid[row][col] == target:
Hint: Type the comparison operator that checks whether the element equals the target.
5. for row in range(len(grid)): for col in range(len(grid[row])): print(grid[row][col])
Hint: Type the keyword that starts the inner loop stepping through each column.

Spot the Error

Question 1
A grid = [[1, 2, 3], [4, 5, 6]]
B for r in range(3):
C for c in range(3):
D print(grid[r][c])
Line B — The grid has only 2 rows, so the outer loop must use range(2). range(3) makes r reach 2, and grid[2] causes an IndexError. It should be: for r in range(2):
Question 2
A grid = [[2, 4], [6, 8]]
B for row in grid:
C total = 1
D for n in row:
E total += n
F print(total)
Line C — The row total must start at 0 for every row. Starting at 1 makes every row total one too high. It should be: total = 0
Question 3
A grid = [[1, 2], [3, 4]]
B sums = 0
C for row in grid:
D for c in range(2):
E sums[c] += row[c]
F print(sums)
Line B — Column totals need one counter per column, so sums must be a list. With sums = 0, sums[c] fails because an integer cannot be indexed. It should be: sums = [0, 0]
Question 4
A grid = [[1, 2], [3, 4]
B for row in grid:
C for x in row:
D print(x, end=' ')
E print()
Line A — The grid is missing its final closing bracket, which is a syntax error. It should be: grid = [[1, 2], [3, 4]]
Question 5
A grid = [[5, 8], [3, 9]]
B found = False
C for r in range(2):
D for r in range(2):
E if grid[r][c] == 9:
F found = True
Line D — The inner loop must use a different variable for the columns. It reuses r, so it overwrites the row index, and c is never defined. It should be: for c in range(2):

Student with a class code? Join your class →