GCSE Python · Lesson 5 of 9

Two-dimensional lists and nested loops

OCR J277 2.2 / AQA 8525 programming

Calculate row totals and the largest score in a grid.

← All GCSE Python lessons

Two-dimensional lists

A two-dimensional list stores rows of related values, such as marks for multiple students. Nested loops can process each row and each item inside it. Sketch a small grid and map each row and column index to the corresponding cell.

Watch out: Not every row must have the same length. Do not assume a fixed number of columns unless the data guarantees it.

1 · Learn and predict

Read this worked example. Predict its output, run it in Python 3, and explain how variables change.

scores = [[5, 8, 7], [4, 9, 6]]
for row in scores:
    total = 0
    for score in row:
        total += score
    print('Row total:', total)

2 · Build independently

Calculate row totals and the largest score in a grid. Work from the starter code before comparing with the solution.

Download starter + solution ZIP ↓

For a stretch challenge: add meaningful test data, handle an edge case and explain one design decision.

Reveal reference solution
scores = [[5, 8, 7], [4, 9, 6], [10, 3, 5]]
row_totals = []
highest = None
for row in scores:
    total = 0
    for score in row:
        total += score
        if highest is None or score > highest:
            highest = score
    row_totals.append(total)
print('Totals:', row_totals)
print('Highest:', highest)

The reference is one possible approach. Test your own program before comparing.

3 · Debug and explain

Question: Why would scores[1][2] select 6 in the example?

Show explanation

Indexing starts at zero: row 1 is the second row; column 2 is its third item.

4 · Check your understanding

What does scores[0][1] access?

5 · Reflect

Explain one test you used, one problem you solved and one improvement you would make.

This records completion only in this browser and does not certify mastery.

Want extra practice? Try interactive Python tracing and debugging questions →

APPLY THE IDEA

Use this skill beyond the example

A two-dimensional list uses two indexes: one for the row and one for an item within that row. Draw the grid and label row and column positions before writing nested loops.

Quick check: What does grid[1][0] select?

Check: The first element of the second row when zero-based indexing is used.