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.
Row totals with nested loops
# 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
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
# 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
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].
Using the points table, find the total for round 2 (the third column).
- Column index 2 holds 2, 8 and 1.
- 2 + 8 + 1 = 11
Answer: 11
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.
Try two quick questions
See if it has sunk in. Answers are marked straight away.
2 more questions
Try each one, then tap to check.
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.
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.
- Pearson Edexcel GCSE Computer Science (1CP2) specification, topic 1.2.2, 6.3.1
- Pearson Edexcel International GCSE Computer Science (4CP0) specification, topic 2.3.2
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? โ