Processing 2D lists and records with loops

Exam programs often load a table of data and ask for totals, the highest value or a count. Nested loops visit every cell of a 2D list.

Practise this lesson free (7 questions)

Row totals with nested loops

The outer loop picks a row; the inner loop visits each column in that row. Reset the total at the start of each row.
# Points scored by 3 players (rows) in 4 rounds (columns)
points = [[4, 7, 2, 9],
[6, 3, 8, 5],
[7, 7, 1, 4]]
for row in range(len(points)):
total = 0
for col in range(len(points[row])):
total = total + points[row][col]
print("Player", row, "total", total)
Output:
Player 0 total 22
Player 1 total 22
Player 2 total 19

Column totals

Swap the loops: the outer loop picks a column and the inner loop goes down the rows.
points = [[4, 7, 2, 9], [6, 3, 8, 5], [7, 7, 1, 4]]
for col in range(4):
total = 0
for row in range(3):
total = total + points[row][col]
print("Round", col, "total", total)
Output:
Round 0 total 17
Round 1 total 17
Round 2 total 11
Round 3 total 18

Lists of records

A record holds related fields of different types. A list of records is like a table: one record per row, one field per column. Use the field's index: s[0] is the name and s[2] is the score.
# Each record: [name, form, score]
students = [["Ana", "10A", 64], ["Ben", "10B", 81], ["Cy", "10A", 77], ["Dee", "10B", 58]]
best = students[0]
for s in students:
if s[2] > best[2]:
best = s
print(best[0], best[2])
count = 0
for s in students:
if s[1] == "10A":
count = count + 1
print(count)
Output:
Ben 81
2

Changing a value

Use two indexes to change one field of one record:
students = [["Ana", "10A", 64], ["Ben", "10B", 81]]
students[1][2] = students[1][2] + 5
print(students[1])
Output: ['Ben', '10B', 86]
Replacing a whole row: students[0] = ["Ana", "10C", 70].
Worked example

Using the points table, find the total for round 2 (the third column).

  1. Column index 2 holds 2, 8 and 1.
  2. 2 + 8 + 1 = 11

Answer: 11

Key idea

Use nested loops for 2D lists: outer loop for rows and inner loop for columns (or the other way round for column totals). A list of records stores one record per row; pick a field with its index.

Quick check

Try two quick questions

See if it has sunk in. Answers are marked straight away.

Extra practice

2 more questions

Try each one, then tap to check.

  1. points = [[4, 7, 2, 9], [6, 3, 8, 5], [7, 7, 1, 4]]. What is points[1][2]?

    Show the answer

    Answer: 8

    points[1] is [6, 3, 8, 5]; index 2 in it is 8.

  2. This program prints a total for each player. What total is printed for Player 1?
    points = [[5, 1, 6, 3],
    [2, 8, 4, 7],
    [9, 3, 5, 2]]
    for row in range(len(points)):
    total = 0
    for col in range(len(points[row])):
    total = total + points[row][col]
    print("Player", row, "total", total)

    Show the answer

    Answer: 21

    Row 1 is [2, 8, 4, 7]: 2 + 8 + 4 + 7 = 21.

Check you have got it

Answer 7 quick questions with instant marking. If you get one wrong, GCSE-ready shows you why and gives you another go. It is free, and you do not need an account.

Video for this lesson coming soon. Subscribe on YouTube

Sources and references

Checked by the GCSE-ready editorial team, 17 October 2026. Last updated 2 October 2026. Spotted a mistake? Tell us and we will fix it.

What do exam command words like explain and evaluate mean? โ†’

Related lessons