All subjects · Computer Science · WCP02 Practical Programming and Problem-solving

Arrays and lists in one and two dimensions

Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving

Python lists are used for arrays. A two-dimensional list (a list of lists) models a table or grid.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Arrays and lists (spec 8.1.2)

An array holds items of one type; a list can hold mixed types. In Python both are written with square brackets, are ordered and mutable, and start at index 0. Negative indexes count from the end.

temps = [12, 15, 9, 20, 17]
print(temps[0], temps[4], temps[-1])
temps[2] = 11
print(temps)
print(len(temps), temps[1:3])

Output:

12 17 17
[12, 15, 11, 20, 17]
5 [15, 11]

Two-dimensional lists

A 2D list is a list of rows. grid[r][c] is row r, column c. The PLS says structures are not ragged: every row has the same length.

grid = [[1, 2, 3],
        [4, 5, 6]]
print(grid[1][0], grid[0][2])
print(len(grid), len(grid[0]))
for row in grid:
    total = 0
    for value in row:
        total = total + value
    print(total)

Output:

4 3
2 3
6
15

Creating a list of a set size

Build a 1D list with a repetition or a loop. For a 2D list, build each row separately: [[0] * 3] * 2 would make two references to the same row.

scores = [0] * 4
board = []
for r in range(2):
    board.append([0] * 3)
board[0][1] = 9
print(scores, board)
shared = [[0] * 3] * 2
shared[0][1] = 9
print(shared)

Output:

[0, 0, 0, 0] [[0, 9, 0], [0, 0, 0]]
[[0, 9, 0], [0, 9, 0]]

Traversing by row and by column

To total a column, keep the column index fixed and loop over the rows:

sales = [[3, 5, 2], [4, 1, 6], [2, 2, 2]]
col = 1
total = 0
for r in range(len(sales)):
    total = total + sales[r][col]
print(total)

Output:

8

Worked example

seats = [["A1", "A2", "A3"], ["B1", "B2", "B3"]]. What are seats[1][2] and seats[0][-1], and how many seats are there?

  1. seats[1] is the second row; index 2 is its third item: B3
  2. seats[0][-1] is the last item of the first row: A3
  3. Rows x columns = 2 x 3 = 6

Answer: B3, A3, 6 seats

Key idea: Lists are indexed from 0 and negative indexes count from the end; a 2D list is a list of rows accessed as grid[row][col]; build each row separately.

Check yourself

  1. What is displayed when this code runs? ```python names = ["Kai", "Lin", "Mo", "Nia"] print(names[2], names[-2]) ```
    Show answer

    Mo Mo. Index 2 is the third item and index -2 is the second from the end; both are Mo: Mo Mo.

  2. What is displayed when this code runs? ```python nums = [10, 20, 30, 40, 50] print(nums[1:4]) ```
    • [20, 30, 40, 50]
    • [10, 20, 30, 40]
    • [20, 30]
    • [20, 30, 40]
    Show answer

    [20, 30, 40]. A slice includes the start index and stops before the end index: [20, 30, 40].

  3. What is displayed when this code runs? ```python m = [[5, 1, 8], [2, 9, 4], [7, 3, 6]] print(m[2][1] + m[0][2]) ```
    Show answer

    11. m[2][1] is 3 and m[0][2] is 8, so 11.

  4. What is the value of total after this code runs? ```python m = [[5, 1, 8], [2, 9, 4], [7, 3, 6]] total = 0 for i in range(3): total = total + m[i][i] ```
    Show answer

    20. m[i][i] is the leading diagonal: 5 + 9 + 6 = 20.

  5. What is displayed when this code runs? ```python rows = [[0] * 2] * 3 rows[0][0] = 1 print(rows) ```
    • [[1, 0], [1, 0], [1, 0]]
    • [[1, 0], [0, 0], [0, 0]]
    • [[1, 1], [0, 0], [0, 0]]
    • [[0, 0], [0, 0], [0, 0]]
    Show answer

    [[1, 0], [1, 0], [1, 0]]. Multiplying the outer list copies the reference, so all three rows are the same list: [[1, 0], [1, 0], [1, 0]].

  6. Which exception is raised when this code runs? ```python data = [[1, 2], [3, 4]] print(data[2][0]) ```
    • KeyError
    • TypeError
    • IndexError
    • ValueError
    Show answer

    IndexError. There are only rows 0 and 1, so data[2] raises IndexError.

  7. This code should print the total of the last column of table. Which line completes it? ```python table = [[1, 2, 3], [4, 5, 6]] total = 0 for r in range(len(table)): __________ print(total) ```
    • `total = total + table[r][2]`
    • `total = total + table[2][r]`
    • `total = total + table[r]`
    • `total = table[r][2]`
    Show answer

    `total = total + table[r][2]`. Fix the column index at 2 and vary the row index r: 3 + 6 = 9.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.