Quarry School

Choose a list or dictionary for a roster

Subject: Computing
Level 1 · FoundationsAbout 4 min read
How this was estimated

Programmatic estimate. Rung levels, worked steps, prerequisites, terms and sentence length contribute to this level. It describes the lesson, not your ability.

Reading counts the whole lesson, including folded explanations, at 180 words a minute. Practice allows two minutes per item. These are planning assumptions; take the time you need.

AI estimate unavailable.

Path to here

Open the unit’s lesson list to follow the lesson order.

    Quick review (2 minutes)

    In plain words

    A class has a seating order and a room for each student.

    Rule

    Use a list when the order or position of values matters, and read it by index. Use a dictionary when values are looked up by a key such as a name, and read it by key. Keys of a dictionary are unique; values and list items may repeat.

    Step

    1. Write the questions the program must answer.

    2. If a question asks by position, such as first or next, keep a list.

    3. If a question asks by a name or code, keep a dictionary with that name as the key.

    4. Check the keys: each key holds one value only.

    5. Use both when the program needs both kinds of question.

    Trap

    Using a name as a list index stops the program: seats["Cy"] raises a TypeError, because a list index is a whole number. Keep a dictionary for lookups by name.

    Using one dictionary for values that share a key loses one of them: storing a second value with the same key replaces the first.

    1 In plain words

    A class has a seating order and a room for each student. The seating order is about position: who sits first, second, third. The rooms are about lookup: which room is Cy's?

    A list fits the first job: its items keep their order and are read by position. A dictionary fits the second: it reads a value by a key, such as a name, without counting along.

    Remember one sentence: position means a list, lookup by name means a dictionary. The choice is this lesson's advice; the two types are described in sections 3.1.3 and 5.5 of the Python 3.13 tutorial.

    2 Picture it

    QuestionChooseRead withWho sits 2nd?listseats[1]Cy's room?dictionaryrooms["Cy"]Who is first?listseats[0]

    A table of three roster questions. Who sits second is answered by the list, read with seats[1]. Cy's room is answered by the dictionary, read with rooms with the key Cy. Who is first is answered by the list, read with seats[0]. The first two rows are marked.

    Which type answers each roster question

    3 Why

    A list of names can say where Cy sits, but it holds no rooms. The plain way to find a room by name is to store the rooms by name: a dictionary keyed by name reads Cy's room directly.

    Each name used as a key holds one room, because keys are unique. Several names can hold the same room, because values can repeat.

    A list allows repeats and keeps order, so it suits a sequence such as a seating plan or a queue.

    4 The rule

    Use a list when the order or position of values matters, and read it by index.

    Use a dictionary when values are looked up by a key such as a name, and read it by key. Keys of a dictionary are unique; values and list items may repeat.

    5 How

    1. Write the questions the program must answer.
    2. If a question asks by position, such as first or next, keep a list.
    3. If a question asks by a name or code, keep a dictionary with that name as the key.
    4. Check the keys: each key holds one value only.
    5. Use both when the program needs both kinds of question.

    6 Worked example

    Keep a seating list and a room dictionary

    Python code
    seats = ["Ada", "Ben", "Cy"]
    rooms = {"Ada": "B2", "Ben": "A1", "Cy": "B2"}
    print(seats[1])
    print(rooms["Cy"])
    seats.append("Dee")
    rooms["Dee"] = "A1"
    print(seats)
    print(rooms["Dee"])
    

    Standard input

    Empty

    Standard output

    Ben
    B2
    ['Ada', 'Ben', 'Cy', 'Dee']
    A1
    

    Predict the four output lines first, then run the file. The list answers who sits second; the dictionary answers Cy's room without counting along. Dee is added to both.

    Trace

    Trace the roster program
    LineReadsOutputExplanation
    print(seats[1])the item at index 1BenPosition 1 is the second seat.
    print(rooms["Cy"])the value of key CyB2The room is read by name.
    seats.append("Dee"), rooms["Dee"] = "A1"——Dee joins the end of the list and becomes a key.
    print(seats)the whole list['Ada', 'Ben', 'Cy', 'Dee']The list keeps its order.
    print(rooms["Dee"])the value of key DeeA1The new pair is read by name.

    7 The trap

    Using a name as a list index stops the program: seats["Cy"] raises a TypeError, because a list index is a whole number. Keep a dictionary for lookups by name.

    Using one dictionary for values that share a key loses one of them: storing a second value with the same key replaces the first.

    Quick checks

    Quick check 1 of 2

    A program must answer two questions about a library: which book is fifth on the returns trolley, and which shelf holds a given title. Which type fits each question?

    Show the steps and the answer
    Answer

    A list for the trolley, and a dictionary from title to shelf.

    Why: The fifth book is a question of position, read with an index. The shelf is looked up by the title, a key.

    Source: Python Software Foundation, Python 3.13 Tutorial, 5.5 Dictionaries

    Quick check 2 of 2

    Predict what this program displays. The program reads no input.

    Python code
    rooms = {"Ada": "B2"}
    rooms["Ada"] = "A1"
    print(rooms)
    Show the steps and the answer
    Answer

    It displays {'Ada': 'A1'}.

    Why: Ada is already a key, so storing again replaces B2 with A1; the dictionary still holds one pair.

    Source: Python Software Foundation, Python 3.13 Tutorial, 5.5 Dictionaries

    Sources

    1. Python 3.13 Tutorial, 3.1.3 Lists. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    2. Python 3.13 Tutorial, 5.5 Dictionaries. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    3. Python 3.13 Library Reference, Built-in Types: Common Sequence Operations. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    4. Python 3.13 Tutorial, 5.1 More on Lists. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    5. Python 3.13 Library Reference, Built-in Exceptions: IndexError. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.