Look up a key
Quick review (2 minutes)
A phone book is not read by position: you look up a name and read the number beside it.
A dictionary holds key: value pairs with unique keys. d[k] gives the value stored with key k, and a missing key raises a KeyError. d[k] = v adds the pair, or replaces the value when k is already a key. k in d is True when k is a key.
Write each key with its value, in pairs, as the dictionary is built.
For
d[k] = v, replace the value ifkis already a key; otherwise add the new pair at the end, since the keys keep the order they were added in.For
d[k], find the key and read its value; if the key is missing, the program stops with a KeyError.For
k in d, answerTruewhenkis one of the keys, andFalseotherwise.
Reading a key that was never stored stops the program with a KeyError. When a key may be missing, test it with in first.
A dictionary is not read by position. phone[0] looks for the key 0, not the first pair, and here it stops with a KeyError because 0 is not one of its keys.
1 In plain words
A phone book is not read by position: you look up a name and read the number beside it.
A dictionary works that way. It holds key: value pairs between curly braces, such as {"Ada": 4098, "Ben": 4139}. Each key appears once, and phone["Ada"] gives the value stored with the key "Ada".
Storing with a key, as in phone["Cy"] = 4127, adds the pair when the key is new and replaces the old value when the key is already there. "Ben" in phone is True when "Ben" is a key.
Remember one sentence: a dictionary finds a value by its key. To look it up, see section 5.5 of the Python 3.13 tutorial, on dictionaries.
2 Picture it
A table of the dictionary phone after the worked example runs. The key Ada holds 4100, the key Ben 4139 and the key Cy 4127. The row for Ada is marked, since its value was replaced.
3 Why
A list is indexed by whole-number positions; a dictionary is indexed by its keys. Strings and numbers can be keys.
Keys are unique within one dictionary, so storing with a key in use forgets the old value. In the worked example, phone["Ada"] = 4100 replaces 4098, and the dictionary still holds three pairs.
Reading a key that is not there stops the program with a KeyError. Test with in first. Printing a dictionary shows its pairs in curly braces, in the order the keys were added.
4 The rule
A dictionary holds key: value pairs with unique keys.
d[k] gives the value stored with key k, and a missing key raises a KeyError. d[k] = v adds the pair, or replaces the value when k is already a key. k in d is True when k is a key.
5 How
- Write each key with its value, in pairs, as the dictionary is built.
- For
d[k] = v, replace the value ifkis already a key; otherwise add the new pair at the end, since the keys keep the order they were added in. - For
d[k], find the key and read its value; if the key is missing, the program stops with a KeyError. - For
k in d, answerTruewhenkis one of the keys, andFalseotherwise.
6 Worked example
Store, replace and read by key
phone = {"Ada": 4098, "Ben": 4139}
phone["Cy"] = 4127
phone["Ada"] = 4100
print(phone["Ada"])
print(phone)
print("Ben" in phone)
Standard input
Empty
Standard output
4100
{'Ada': 4100, 'Ben': 4139, 'Cy': 4127}
True
Predict the three output lines first, then run the file. Cy is a new key, so its pair is added; Ada is already a key, so its value is replaced. Python prints the text keys with single quotation marks.
Read a key that is not there
phone = {"Ada": 4098}
print(phone["Dee"])
Standard input
Empty
Standard output
Empty
Exception KeyError
Save the two lines as dee.py and run the file. Dee is not a key, so the program stops with a KeyError.
Trace
| Line | phone | Output | Explanation |
|---|---|---|---|
phone = {...} | Ada 4098, Ben 4139 | — | Two pairs. |
phone["Cy"] = 4127 | Ada 4098, Ben 4139, Cy 4127 | — | Cy is new, so the pair is added. |
phone["Ada"] = 4100 | Ada 4100, Ben 4139, Cy 4127 | — | Ada is already a key, so 4098 is replaced. |
print(phone["Ada"]) | unchanged | 4100 | The value stored with Ada. |
print(phone) | unchanged | {'Ada': 4100, 'Ben': 4139, 'Cy': 4127} | The pairs are printed in curly braces, in the order the keys were added. |
print("Ben" in phone) | unchanged | True | Ben is a key. |
7 The trap
Reading a key that was never stored stops the program with a KeyError. When a key may be missing, test it with in first.
A dictionary is not read by position. phone[0] looks for the key 0, not the first pair, and here it stops with a KeyError because 0 is not one of its keys.
Quick checks
Quick check 1 of 2
Predict what this program displays. The program reads no input.
stock = {"pens": 4, "cups": 2}
stock["pens"] = 9
stock["jars"] = 1
print(stock["pens"])
print(stock)Show the steps and the answer
It displays 9 and then {'pens': 9, 'cups': 2, 'jars': 1}.
Why: pens is already a key, so its value becomes 9 and it keeps its place. jars is new, so a third pair is added at the end.
Source: Python Software Foundation, Python 3.13 Tutorial, 5.5 Dictionaries
Quick check 2 of 2
ages = {"Ada": 12}. What does print("Ben" in ages) display, and what does print(ages["Ben"]) do?
Show the steps and the answer
The first displays False. The second stops the program with a KeyError.
Why: Ben is not a key. in answers the question; reading the missing key raises KeyError.
Source: Python Software Foundation, Python 3.13 Tutorial, 5.5 Dictionaries
Sources
- 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 Exceptions: KeyError. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.