Edexcel International Advanced Level Computer Science, point 17.1.3
The structures are introduced in 4.2.1 and used in code in 8.1.2 and 8.2.4. Here the skill is choosing the best one for a problem, in one or two dimensions. Check the PLS for the exact features.
Decision guide
| Question | Structure |
|---|---|
| A collection in order, which changes in size? | List |
| A fixed number of same-type items accessed by position? | List used as an array (fixed length) |
| A group that must not change? | Tuple |
| Look up a value from a key (name, ID, word)? | Dictionary |
| Only unique items, with fast membership and set operations? | Set |
| One thing with several named fields? | Record (dictionary, class or dataclass) |
| Last in, first out? | Stack (list with append and pop) |
| First in, first out? | Queue (collections.deque, or a circular list, 18.2.1) |
| A table or grid? | Two-dimensional list (list of lists) |
| Many records to look up by ID? | Dictionary of records |
One-dimensional structures
scores = [72, 85, 90] # list / 1-D array
point = (3, 4) # tuple: fixed
ages = {"Amy": 17, "Raj": 18} # dictionary: look up by name
seen = set() # unique items
Access: by index (scores[1]), by key (ages["Raj"]), or membership (3 in seen).
Two-dimensional structures
A two-dimensional list holds rows of values and is used for grids, tables and boards: grid[row][col].
ROWS, COLS = 3, 4
grid = [[0] * COLS for _ in range(ROWS)] # 3 rows x 4 columns of 0
grid[1][2] = 5 # row 1, column 2
for r in range(ROWS): # traverse every cell
for c in range(COLS):
print(grid[r][c], end=" ")
print()
Mistakes to avoid: [[0] * 4] * 3 makes three references to the same row, so changing one cell changes a whole column. Build each row separately. Row first, then column (grid[row][col]).
Using a 2D list for different jobs:
| Job | What the cells hold |
|---|---|
| Noughts and crosses board | " ", "X", "O" |
| A maze | Walls, paths, start, end |
| A table of marks | One row per student, one column per test |
| Seats in a theatre | Booked or free |
| A matrix (graph, 18.2.6) | Weights |
Row and column operations:
marks = [[60, 70, 80],
[55, 65, 75]]
row_totals = [sum(row) for row in marks] # [210, 195]
# [115, 135, 155]
col_totals = [sum(col) for col in zip(*marks)]
print(row_totals, col_totals)
One structure or several?
Sometimes a combination is best: a dictionary whose values are lists (a student's marks), or a list of dictionaries (records):
students = [
{"name": "Amy", "marks": [60, 70, 80]},
{"name": "Raj", "marks": [55, 65, 75]},
]
for s in students:
# 70.0 and 65.0
print(s["name"], sum(s["marks"]) / len(s["marks"]))
Worked example
Task: a program stores a noughts and crosses board, places a move, and checks for a win in any row.
board = [[" "] * 3 for _ in range(3)]
def place(board, row, col, player):
if board[row][col] != " ":
return False # square taken
board[row][col] = player
return True
def row_win(board, player):
return any(all(cell == player for cell in row) for row in board)
place(board, 0, 0, "X")
place(board, 0, 1, "X")
place(board, 0, 2, "X")
print(row_win(board, "X")) # True
print(place(board, 0, 0, "O")) # False: already taken
Why a 2D list: the board is a grid, so board[row][col] matches how people think about it, and loops over rows and columns are simple. The same structure suits a seating plan (pattern recognition, 10.1.3).
Exam tip: say why a structure fits ("a dictionary because items are looked up by name"). For grids, use a 2D list, with [row][col] indexing, and build the rows separately. Check the index limits (0 to len - 1).
Check yourself
Which structure would you use to look up a student's mark by name?
A dictionary, with the name as the key.
How do you create a 3 by 4 grid of zeros safely?
[[0] * 4 for _ in range(3)].
Why does [[0] * 4] * 3 cause problems?
It creates three references to the same row, so changing one cell changes the same column in every row.
What does grid[2][1] refer to?
The item in row 2, column 1 (counting from 0).