Choose a list or dictionary for a roster
Quick review (2 minutes)
A class has a seating order and a room for each student.
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.
Write the questions the program must answer.
If a question asks by position, such as first or next, keep a list.
If a question asks by a name or code, keep a dictionary with that name as the key.
Check the keys: each key holds one value only.
Use both when the program needs both kinds of question.
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
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.
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
- Write the questions the program must answer.
- If a question asks by position, such as first or next, keep a list.
- If a question asks by a name or code, keep a dictionary with that name as the key.
- Check the keys: each key holds one value only.
- Use both when the program needs both kinds of question.
6 Worked example
Keep a seating list and a room dictionary
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
| Line | Reads | Output | Explanation |
|---|---|---|---|
print(seats[1]) | the item at index 1 | Ben | Position 1 is the second seat. |
print(rooms["Cy"]) | the value of key Cy | B2 | The 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 Dee | A1 | The 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
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
Sources
- Python 3.13 Tutorial, 3.1.3 Lists. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Tutorial, 5.5 Dictionaries. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Library Reference, Built-in Types: Common Sequence Operations. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Tutorial, 5.1 More on Lists. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Library Reference, Built-in Exceptions: IndexError. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.