Community resourceWorksheet

1CP2-CT-4.3 Traversing two-dimensional lists

Part 3 of 7 · 1CP2-CT-4 · Structures, validation and search

The traversal worksheet, where the outer loop chooses the row and the inner loop chooses the field.

Students will:

  • calculate how many times an inner-loop body executes
  • trace a nested traversal that accumulates a total
  • process one field from every record
  • choose the smallest traversal a requirement actually needs
  • explain the effect of resetting a total inside the row loop

Inside: 6 explanation cells, 2 multiple-choice questions, 1 Python task, 1 trace table, 1 fill-in-the-blanks cell and 2 written answers. 14 marks, about 45 minutes.

Series: 1CP2-CT-4 · Structures, validation and search, part 3 of 7.

Shared by Coding PathwayVerified teacher

  • 13 cells
  • About 45 minutes
  • CC BY-SA 4.0
  • Shared 17 Aug 2026

Preview

The whole resource, exactly as a class sees it. Answers and marking are held back.

Traversing two-dimensional lists

To traverse a structure is to visit its items systematically. A two-dimensional structure normally needs a loop for its rows and another loop for the fields inside each row.

1. Outer and inner loops

Nested traversal of a two-dimensional structureOuter loop: choose a row · Inner loop: visit its fieldsrow 0, field 0row 0, field 1row 0, field 2row 1, field 0row 1, field 1row 1, field 2row 2, field 0row 2, field 1row 2, field 2finish rowthen move down

The outer loop selects one row. The inner loop completes every field in that row. Only then does the outer loop move to the next row.

for row in table:
    for value in row:
        print(value)
Fill in the blanks3 marks
The gap 1 loop selects each row. The gap 2 loop visits fields within that row. This systematic visit is called gap 3.
  • inner
  • outer
  • selection
  • traversal
Multiple choice1 mark

A rectangular structure has 4 rows and 3 fields per row. How many times does the inner-loop body execute during a full traversal?

  • A3
  • B4
  • C7
  • D12

2. Trace row-by-row accumulation

The algorithm adds every value. Record the changing row, value and total. Blank boxes mean unchanged.

Trace table4 marks

Trace the nested traversal and complete the table.

Follow the inner loop across each row before moving to the next.

Use one row for each pass through the loop. Fill in a box only when that value changes on that row, and leave the rest blank.

ProgramPython
  1. numbers = [[2, 4], [3, 5]]
  2. total = 0
  3. for row in numbers:
  4. for value in row:
  5. total = total + value
  6. print(total)
Trace table with 5 columns
RownumberstotalrowvalueOutput
1
2
3
4
5
6
Written answer2 marks

Explain why the final total is 14 and how the nested loops produce it.

Refer to both loop roles and the four visited values.

Students type their answer here.

3. Process one field from every record

sales contains [product, units] records. Use a loop to add field 1 from every row into total_units. Print the total. You do not need an inner loop when only one known field is required.

Coding task5 marks
sales = [["Pen", 12], ["Pad", 7], ["File", 9]]
# Add the units field from every row and print the total.

4. Choose the smallest suitable traversal

Use nested loops when every field must be processed. Use one row loop plus a fixed field index when only one field from each record is needed. Extra loops increase cognitive load and can accidentally process values that are irrelevant.

Written answer2 marks

Compare the traversal needed to print every field with the traversal needed to total only field 2 from each record.

State the loop structure and indexing for each purpose.

Students type their answer here.

Multiple choice1 mark

What is the effect of placing total = 0 inside the row loop when calculating a grand total?

  • AIt makes every field a string.
  • BIt resets the accumulator for each row, losing earlier rows.
  • CIt creates an extra field.
  • DIt automatically validates the values.

Route forward

You can traverse a rectangular structure and choose between full nested traversal and one-field row processing. Next you will stop unsuitable input before it enters such structures.