Quarry School

Look up a key

Subject: Computing
Level 1 · FoundationsAbout 4 min read2 new terms
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 phone book is not read by position: you look up a name and read the number beside it.

    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.

    Step

    1. Write each key with its value, in pairs, as the dictionary is built.

    2. For d[k] = v, replace the value if k is already a key; otherwise add the new pair at the end, since the keys keep the order they were added in.

    3. For d[k], find the key and read its value; if the key is missing, the program stops with a KeyError.

    4. For k in d, answer True when k is one of the keys, and False otherwise.

    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.

    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

    KeyValueAda4100Ben4139Cy4127

    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.

    The phone dictionary after the worked example's lines

    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

    1. Write each key with its value, in pairs, as the dictionary is built.
    2. For d[k] = v, replace the value if k is already a key; otherwise add the new pair at the end, since the keys keep the order they were added in.
    3. For d[k], find the key and read its value; if the key is missing, the program stops with a KeyError.
    4. For k in d, answer True when k is one of the keys, and False otherwise.

    6 Worked example

    Store, replace and read by key

    Python code
    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

    Python code
    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

    Trace the phone book program
    LinephoneOutputExplanation
    phone = {...}Ada 4098, Ben 4139—Two pairs.
    phone["Cy"] = 4127Ada 4098, Ben 4139, Cy 4127—Cy is new, so the pair is added.
    phone["Ada"] = 4100Ada 4100, Ben 4139, Cy 4127—Ada is already a key, so 4098 is replaced.
    print(phone["Ada"])unchanged4100The 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)unchangedTrueBen 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.

    Python code
    stock = {"pens": 4, "cups": 2}
    stock["pens"] = 9
    stock["jars"] = 1
    print(stock["pens"])
    print(stock)
    Show the steps and the answer
    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
    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

    1. Python 3.13 Tutorial, 5.5 Dictionaries. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    2. Python 3.13 Library Reference, Built-in Exceptions: KeyError. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.