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.
The built-in structures
Each structure has a different purpose.
| Structure | Example | Ordered? | Mutable? | Duplicates? |
|---|---|---|---|---|
| list | [3, 1, 3] | Yes (by index) | Yes | Yes |
| tuple | (3, 1, 3) | Yes (by index) | No | Yes |
| dict | {'pen': 12} | Keeps insertion order | Yes | Keys unique |
| set | {1, 3} | No | Yes | No |
| str | 'cat' | Yes (by index) | No | Yes |
Records, sets and dictionaries
- A record groups related fields about one item. The PLS describes a record as a list of mixed-type fields, such as ['Ana', 17, True]; a dictionary ({'name': 'Ana', 'score': 71}), a tuple or a class (see the OOP lessons) also work. A list of records is a table.
- A one-item tuple needs a trailing comma: (5,) is a tuple, but (5) is just the int 5.
- Sets test membership fast and support | (union), & (intersection), - (difference) and ^ (symmetric difference). s.remove(x) raises KeyError if x is missing; s.discard(x) does nothing.
- A dictionary looks values up by key. d.get(key, default) avoids a KeyError when the key may be missing; d.keys(), d.values() and d.items() let you loop.
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 noneSorting
- items.sort() sorts a list in place and returns None; sorted(items) returns a new list and leaves the original unchanged. Both accept reverse=True and key=function.
- The default order is case sensitive: every capital letter comes before every lower-case letter. For a case-insensitive sort pass the function itself, key=str.lower, with no brackets. (The PLS prints this as key=str.lower(), but in real Python the brackets call the method with no string and raise TypeError.)
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 earlyTwo-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.
- ```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)) ```
- 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.
Check yourself
- 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`
- 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`.
- 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]]`
- 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]`
- 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`.
- 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.
- 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]`
- 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`
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Computer Science specification, Issue 1 (March 2026)
- Pearson Edexcel IAL Computer Science Unit 4 Programming Language Subset (PLS)
- Pearson Edexcel IAL Computer Science sample assessment materials (Unit 3 and Unit 4)
Last updated 8 November 2026. Spotted a mistake? Tell us.