Traversing a 2D array with nested loops

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Traversing a 2D array with nested loops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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])
2. In grid[row][col], which index picks the row?
A The second index, col
B The first index, 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
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)
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1for row in range(len(grid)): for col in range(len(grid[row])): AThe 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].
2total += grid[row][col] Bfor r in range(len(grid)): for c in range(len(grid[r])): if grid[r][c] == target:
3Print a grid row by row: the inner loop builds one row, then the outer loop moves to the next line Cfor row in grid: for item in row: print(item, end=' ') print()
4Find a value in a grid by checking every element with nested loops DAdds the current element to a running total while the nested loops visit each cell
5for row in range(3): for col in range(4): print(grid[col][row]) Etotal 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.
6for row in range(len(grid)): total = 0 for col in range(len(grid[row])): total += grid[row][col] total_all = total FOuter loop steps through each row and the inner loop steps through each column in that row, so every element is visited

Definitions

grid[row][col]

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
for row in range(___(grid)): for col in range(len(grid[row])): print(grid[row][col])
2.
for row in range(3): for col in range(3): totals[col] += grid[___][col]
3.
for row in range(len(grid)): for col in range(len(grid[row])): print(grid[row][col], ___=' ')
4.
for row in range(len(grid)): for col in range(len(grid[row])): if grid[row][col] __ target:
5.
for row in range(len(grid)): ___ col in range(len(grid[row])): print(grid[row][col])

Spot the Error

Shade in the letter of the line that contains an 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])
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)
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)
Question 4
A grid = [[1, 2], [3, 4]
B for row in grid:
C for x in row:
D print(x, end=' ')
E print()
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

Answer Key

Traversing a 2D array with nested loops
Section A: Quick Questions
Q1
A — for row in range(len(grid)): for col in range(len(grid[row])): print(grid[row][col])
Q2
B — The first index, row
Q3
B — The inner loop is indented inside the outer loop
Q4
A — 12 times (3 x 4)
Q5
A — Inside the outer loop, before the inner loop starts
Section B: Match the Pairs
Q1
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
Q2
total += grid[row][col] → Adds the current element to a running total while the nested loops visit each cell
Q3
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()
Q4
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:
Q5
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].
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
for row in range(len(grid)): for col in range(len(grid[row])): print(grid[row][col])
Q2
for row in range(3): for col in range(3): totals[col] += grid[row][col]
Q3
for row in range(len(grid)): for col in range(len(grid[row])): print(grid[row][col], end=' ')
Q4
for row in range(len(grid)): for col in range(len(grid[row])): if grid[row][col] == target:
Q5
for row in range(len(grid)): for col in range(len(grid[row])): print(grid[row][col])
Section E: Spot the Error
Q1
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):
Q2
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
Q3
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]
Q4
A — The grid is missing its final closing bracket, which is a syntax error. It should be: grid = [[1, 2], [3, 4]]
Q5
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):