GCSE Computer Science

Python 2D lists โ€” GCSE Computer Science

What is a 2D list?

A 2D list is a list of lists โ€” each item in the outer list is itself a list. It's the standard way to represent a grid: a seating plan, a noughts-and-crosses board, a spreadsheet of scores. Instead of one long list, you get rows, and each row is a list of its own.

grid = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

Indexing with two indices

To reach a single value in a 2D list, you need two indices: one for the row, one for the column โ€” grid[row][col]. grid[1][2] means: go to row 1 (the second row, since indexing starts at 0), then column 2 (the third value in that row).

grid = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
print(grid[1][2])   # 6
print(grid[0][0])   # 1
print(grid[2][1])   # 8

Iterating with nested loops

To visit every value in a 2D list, you need a loop inside a loop โ€” one for the rows, one for the columns within each row.

grid = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
for row in grid:
    for value in row:
        print(value)

The outer loop takes each row in turn. The inner loop then takes each value within that row. Together, they visit all nine values, row by row, left to right.

A worked example: a 3×3 grid

grid = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
total = 0
for row_index in range(3):
    for col_index in range(3):
        total = total + grid[row_index][col_index]
print(total)   # 45

This does the same job as the row-by-row loop above, but using index numbers directly โ€” useful when you need to know which row and column a value came from, not just the value itself.

Common exam mistake

Reversing the row/column index order. my_list[1][2] means row 1, column 2 โ€” not column 1, row 2. Exam questions that describe a grid and ask for a specific cell are designed to test this.

What examiners actually test

2D list questions typically describe a grid โ€” a seating plan, a set of scores โ€” and ask you to retrieve, update, or sum values at specific positions, or to write nested loops that process every cell. Read the row/column order carefully in the question: grid[row][col] is consistent throughout Python, but it's easy to swap the two under exam pressure.

Row/column mix-ups are an easy trap, and the only real fix is enough grid questions that the order becomes automatic. Python Coach gives your class 195 challenges across 27 lessons to get there, with progress tracking so you can see who's still counting on their fingers. Sixty teaching days free, no payment details required.

Start your school's free trial →