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

Records, dictionaries and tuples

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

Different structures suit different data. Records group the fields of one item, dictionaries look values up by key, and tuples hold fixed groups that must not change.

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

Practise this lesson

Records (spec 8.1.2)

In the Programming Language Subset a record is a list holding fields of mixed types, with the same number of fields in every record. A table of records is a 2D list.

pupils = [["Ava", 16, 72.5, True],
          ["Raj", 17, 64.0, False]]
for p in pupils:
    if p[3]:
        print(p[0], "has paid; mark", p[2])
NAME = 0
print(pupils[1][NAME])

Output:

Ava has paid; mark 72.5
Raj

Named constants for field positions (NAME = 0) make record code readable.

Dictionaries

A dictionary stores key: value pairs. Keys are unique; values are found by key, not by position. Dictionaries are mutable and keep insertion order.

stock = {"pen": 40, "pad": 12}
stock["ruler"] = 8
stock["pen"] = stock["pen"] - 5
print(stock["pen"], len(stock))
print(stock.get("glue", 0))
for item, qty in stock.items():
    print(item, qty)
print(list(stock.keys()))

Output:

35 3
0
pen 35
pad 12
ruler 8
['pen', 'pad', 'ruler']

d[key] raises KeyError for a missing key; d.get(key, default) returns the default instead.

Tuples

A tuple is ordered like a list but immutable: it cannot be changed after creation. Use one for fixed data such as coordinates or the days of the week. A one-item tuple needs a trailing comma.

point = (3, 7)
x, y = point
print(x + y, point[1])
single = (5,)
print(type(single), type((5)))

Output:

10 7
<class 'tuple'> <class 'int'>

Converting between structures

list(), tuple(), set() and dict() convert between structures. Converting to a set removes duplicates (and loses order).

letters = list("noon")
print(letters)
print(tuple(letters))
print(sorted(set(letters)))
pairs = [("a", 1), ("b", 2)]
print(dict(pairs))
print(list({"x": 1, "y": 2}.values()))

Output:

['n', 'o', 'o', 'n']
('n', 'o', 'o', 'n')
['n', 'o']
{'a': 1, 'b': 2}
[1, 2]

Worked example

Choose a structure for: (a) a fixed set of RGB values (b) a phone book looking up numbers by name (c) the names of students in a queue that changes.

  1. (a) fixed and must not change: tuple
  2. (b) look up by a unique key: dictionary
  3. (c) ordered and changing: list

Answer: tuple, dictionary, list

Key idea: A record is a list of mixed fields; a dictionary maps unique keys to values; a tuple is an immutable ordered group; convert with list(), tuple(), set(), dict().

Check yourself

  1. What is displayed when this code runs? ```python ages = {"Ali": 15, "Bo": 17} ages["Ali"] = ages["Ali"] + 1 ages["Cy"] = 14 print(ages["Ali"], len(ages)) ```
    Show answer

    16 3. Ali's age becomes 16 and a third key is added: 16 3.

  2. What is displayed when this code runs? ```python prices = {"tea": 1.5, "cake": 2.25} print(prices.get("coffee", "n/a")) ```
    • KeyError
    • n/a
    • None
    • coffee
    Show answer

    n/a. get() returns the default when the key is missing: n/a.

  3. Which exception is raised when this code runs? ```python colour = (255, 128, 0) colour[0] = 200 ```
    • IndexError
    • ValueError
    • KeyError
    • TypeError
    Show answer

    TypeError. Tuples are immutable, so assigning to an item raises TypeError.

  4. What is displayed when this code runs? ```python books = [["Dune", 1965, 4.5], ["Emma", 1815, 4.1], ["Kim", 1901, 3.9]] best = books[0] for b in books: if b[2] > best[2]: best = b print(best[0]) ```
    Show answer

    Dune. The loop keeps the record with the highest rating (field 2): Dune.

  5. What is displayed when this code runs? ```python tally = {"a": 3, "b": 5, "c": 2} total = 0 for key, value in tally.items(): if key != "b": total = total + value print(total) ```
    Show answer

    5. Values for a and c are added: 3 + 2 = 5.

  6. What is displayed when this code runs? ```python words = ["red", "blue", "red", "green", "blue"] print(len(set(words))) ```
    Show answer

    3. Converting to a set removes duplicates, leaving 3 different words: 3.

  7. A program stores each country's capital so it can be looked up quickly by country name. Which structure is most suitable?
    • Tuple
    • Set
    • Dictionary
    • Two-dimensional list
    Show answer

    Dictionary. Each country is a unique key and the capital is its value.

  8. This code should display ['pear'] but it does not. Which line number contains the error? ``` 1 inventory = {"apple": 3, "pear": 0, "plum": 5} 2 empty = [] 3 for fruit in inventory: 4 if inventory[fruit] == 0: 5 empty.append(inventory[fruit]) 6 print(empty) ```
    Show answer

    5. Line 5 appends the value (0) instead of the key. It should be empty.append(fruit).

Practise with instant feedback

Sources

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