Start learning free

LESSON 128 OF 673 · DICTIONARIES

Dictionary get() Default Value in Python

Lesson Read the full explanation

TL ; DR:

d.get(key, 0) gives the key's value, or 0 when the key is missing. There is no error either way.


points = {"Ana": 7}

print(points.get("Ana", 0))   # 7
print(points.get("Ben", 0))   # 0


Full lesson:

A missing key often means "none yet". A player with no points yet has 0 points.

points["Ben"] can't say that. It stops the program with a KeyError.

get takes a key and a default. It gives the key's value when the key is there. It gives the default when it isn't.

Reading this way doesn't add the key. points still has one key afterwards.

Without a default, get gives None for a missing key.

It does what this if with in does, in one line:

score = 0
if "Ben" in points:
    score = points["Ben"]

Your task

stock counts the fruit in a shop. The code reads and prints the pears.

There are no plums yet. Read "plums" with get and 0 as the default. Store it in plums.

Then print the plums line, the same way as the pears line.

Expected output:

We have 2 pears
We have 0 plums
STEP 1 OF 2
    Ctrl + Enter to check

    PYTHON · DICTIONARIES

    Dictionary Default Value in Python, explained

    A missing key often means none yet

    A shop with no plums has 0 plums, so a missing key is not always a problem. Reading it with square brackets cannot say that. With stock = {"apples": 5}, the expression stock["plums"] stops the program with KeyError: 'plums'.

    get with a default

    d.get(key, default) takes a key and a default value. It gives the key's value when the key is in the dictionary and the default when it isn't. There is no error either way.

    stock = {"apples": 5}
    print(stock.get("apples", 0))
    print(stock.get("plums", 0))
    print(len(stock))
    
    5
    0
    1
    

    Reading with get does not add the key: stock still has one key afterwards. The default is only the answer to this one call.

    The default is optional. Without it, get gives None for a missing key, so stock.get("plums") is None. That is useful when you want to tell "missing" apart from every real value, but for counts a default of 0 is usually what you want.

    The same thing with an if

    Before get, you would write the check by hand, using in:

    plums = 0
    if "plums" in stock:
        plums = stock["plums"]
    

    Four lines become one: plums = stock.get("plums", 0). The first form is still worth knowing, since it is the right shape when the missing case needs more than a default value, such as printing a message.

    The lesson's task

    The starter code already reads the pears with square brackets, because that key is there, and prints the first line. The new part is the plums: there are none in stock, so read "plums" with get and 0 as the default, store the answer in plums, and print it the same way as the pears line.

    stock = {"apples": 5, "pears": 2}
    pears = stock["pears"]
    print(f"We have {pears} pears")
    plums = stock.get("plums", 0)
    print(f"We have {plums} plums")
    
    We have 2 pears
    We have 0 plums
    

    The text inside f"..." is an f-string: the name in braces is replaced by its value when the line is printed.

    Defaults that are not numbers

    The default can be any value. ages.get("Ben", "unknown") gives the text unknown when Ben isn't in ages. A very common use is counting: read the old count with a default of 0, add 1, and store it back.

    words = ["a", "b", "a", "c", "a"]
    counts = {}
    for w in words:
        counts[w] = counts.get(w, 0) + 1
    print(counts)
    
    {'a': 3, 'b': 1, 'c': 1}
    

    The first time a word shows up, get returns 0, so the word is stored with a count of 1. Every later time, it returns the count so far and the store replaces it with one more.

    Common mistakes

    • Using brackets for a key that may be missing. stock["plums"] raises a KeyError. Use brackets only when you know the key is there, as with the pears.
    • Using the wrong default. The default is what you get back for a missing key. For counts, use 0. Forgetting it gives None, and the line would read We have None plums.
    • Expecting get to store the default. It doesn't. Even after stock.get("plums", 0), the key "plums" is not in stock, and len(stock) is unchanged. To store a missing key's default, assign it yourself.
    • Doing arithmetic on None. stock.get("plums") + 1 fails with a TypeError, while stock.get("plums", 0) + 1 is 1.

    Key takeaways

    • d.get(key, default) returns the value for key, or default when it is missing.
    • It never raises a KeyError and never adds a key.
    • Without a default it returns None.
    Keep going with Python500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free