All subjects · Computer Science · WCP04 Advanced Practical Programming and Problem-solving

One- and two-dimensional data structures

Edexcel IAL Computer Science · WCP04 Advanced Practical Programming and Problem-solving

Choosing the right structure is half the design of a program. This lesson compares Python's built-in structures and shows how to build and process two-dimensional data safely.

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

Practise this lesson

The built-in structures

Each structure has a different purpose.

StructureExampleOrdered?Mutable?Duplicates?
list[3, 1, 3]Yes (by index)YesYes
tuple(3, 1, 3)Yes (by index)NoYes
dict{'pen': 12}Keeps insertion orderYesKeys unique
set{1, 3}NoYesNo
str'cat'Yes (by index)NoYes

Records, sets and dictionaries

a = {1, 2, 3, 4}
b = {3, 4, 5}
print(a | b, a & b, a - b, a ^ b)
stock = {"pen": 12, "ruler": 0}
print(stock.get("pen"), stock.get("glue", "none"))

Output:

{1, 2, 3, 4, 5} {3, 4} {1, 2} {1, 2, 5}
12 none

Sorting

words = ["bat", "Cat", "ant"]
print(sorted(words), sorted(words, key=str.lower), words)
scores = [64, 88, 71]
scores.sort(reverse=True)
print(scores)
try:
    sorted(words, key=str.lower())
except TypeError:
    print("TypeError: key=str.lower() calls the method too early")

Output:

['Cat', 'ant', 'bat'] ['ant', 'bat', 'Cat'] ['bat', 'Cat', 'ant']
[88, 71, 64]
TypeError: key=str.lower() calls the method too early

Two-dimensional lists

A 2D list is a list of rows. grid[r][c] is row r, column c. len(grid) is the number of rows; len(grid[0]) the number of columns. Use nested loops, or a comprehension, to process every cell.

grid = [[1, 2, 3],
        [4, 5, 6]]
print(grid[1][0], len(grid), len(grid[0]))
col_sums = [sum(row[c] for row in grid) for c in range(3)]
print(col_sums)

Output:

4 2 3
[5, 7, 9]

The aliasing trap

[[0] * 3] * 2 copies a reference to the same row twice, so changing one row changes both. Build each row separately with a comprehension.

bad = [[0] * 3] * 2
good = [[0] * 3 for _ in range(2)]
bad[0][0] = 9
good[0][0] = 9
print(bad)
print(good)

Output:

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

Worked example

Scores for 3 students in 4 tests are stored in a 2D list. Print each student's total and the class's highest single score.

  1. ```python scores = [[55, 61, 70, 48], [80, 72, 91, 85], [66, 59, 73, 70]] for number, row in enumerate(scores, start=1): print("Student", number, "total", sum(row)) print("Highest:", max(max(row) for row in scores)) ```
  2. Output: ``` Student 1 total 234 Student 2 total 328 Student 3 total 268 Highest: 91 ```

Answer: Loop over rows for per-student totals; take the maximum of each row's maximum for the overall highest.

Key idea: list (mutable, ordered), tuple (immutable), dict (key to value), set (unique, fast membership), str (immutable). grid[r][c]; build 2D lists with a comprehension, never [[0] * n] * m.

Check yourself

  1. What does this program print? ```python t = [[2, 4, 6], [8, 10, 12], [14, 16, 18]] print(t[2][1], t[0][2]) ```
    Show answer

    16 6. t[2] is [14, 16, 18], so t[2][1] = 16. t[0] is [2, 4, 6], so t[0][2] = 6. Output: `16 6`

  2. What does this program print? ```python t = [[2, 4, 6], [8, 10, 12]] print(sum(t[r][1] for r in range(len(t)))) ```
    Show answer

    14. Column 1 holds 4 and 10, so the sum is `14`.

  3. What does this program print? ```python rows = [[0] * 2] * 2 rows[1][0] = 5 print(rows) ```
    Show answer

    [[5, 0], [5, 0]]. Both rows are the same list object, so setting rows[1][0] also changes rows[0][0]. Output: `[[5, 0], [5, 0]]`

  4. What does this program print? ```python a = {2, 4, 6, 8} b = {6, 8, 10} print(sorted(a ^ b)) ```
    Show answer

    [2, 4, 10]. Symmetric difference keeps items in exactly one set: 2, 4 and 10. Output: `[2, 4, 10]`

  5. What does this program print? ```python ages = {"Ali": 17, "Bea": 18} print(ages.get("Cal", 0) + ages["Bea"]) ```
    Show answer

    18. 'Cal' is missing, so get returns the default 0; 0 + 18 = `18`.

  6. A program stores the fixed coordinates of a map's four corners. Which structure is most suitable?
    • A set, because coordinates are unique
    • A dictionary with the coordinates as values only
    • A tuple of tuples, because the values must not change
    • A string of comma-separated numbers
    Show answer

    A tuple of tuples, because the values must not change. Tuples are immutable, so the corners cannot be changed by accident, and they keep their order.

  7. What does this program print? ```python q = [5, 7, 9] q.pop(0) q.append(1) print(q) ```
    • [7, 9, 1]
    • [5, 7, 9, 1]
    • [7, 9]
    • [5, 7, 1]
    Show answer

    [7, 9, 1]. pop(0) removes the first item (5); append(1) adds to the end. Output: `[7, 9, 1]`

  8. What does this program print? ```python fruit = ["pear", "Plum", "apple"] print(sorted(fruit)[0], sorted(fruit, key=str.lower)[0]) ```
    Show answer

    Plum apple. The default sort is case sensitive, so the capital P of Plum comes before every lower-case letter; with key=str.lower the words are compared as 'pear', 'plum' and 'apple'. Output: `Plum apple`

Practise with instant feedback

Sources

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