Dictionary Default Values
Here’s a code snippet showing the example code used in this lesson:
apples = {"Mahdi": 5}
# Bad: Test and fail
# def get_apples(apples_dict, name):
# try:
# num_apples = apples_dict[name]
# except KeyError:
# num_apples = 0
# return num_apples
# print(get_apples(apples, "Mahdi"))
# print(get_apples(apples, "Jennifer"))
# Good: dict.get() with a default value
his_apples = apples.get("Mahdi", 0)
her_apples = apples.get("Jennifer", 0)
print(his_apples)
print(her_apples)
00:00
In this lesson, I want to talk to you about how you can access values from dictionaries where you don’t know whether the key even exists. Now, for example, what you could do is you could write some quite involved code logic where you try to make that call to the dictionary, passing in the key, and if you get a KeyError
, so if the key doesn’t exist, then you just return a different value instead. And yeah, you could do that, for example, and just pass in the dictionary and then the key.
00:30
And if the key exists, then you’re going to get the number of apples for "Mahdi"
. That would be 5
. And if the key doesn’t exist—in the case of "Jennifer"
, who is not in the dictionary—it would return the value 0
. Now, this works. Well, let’s give it a spin.
00:45
So if you run this, you get 5
and 0
for these two calls to that function. But it is rather involved, and there’s an easier way of doing that in Python.
00:56 This should be a throwback for you to an earlier lesson, where I already told you that if there is a method or a function in Python that is built-in and already does what you want it to do, then that’s what you should use.
01:09
And dictionaries have such a method that’s called .get()
, and .get()
takes as an input a key in the dictionary and optionally also a default value.
01:19
Which means that if you use this .get()
method on the dictionary and the key is present, it’s going to return the value at that key. And if it isn’t present, as is the case for "Jennifer"
, who is not inside of this apples
dictionary, then it’s going to return the default value that you passed in here, which is 0
. So with these two lines of code, you’re going to get the same result as previously. So if I run this, you also get 5
and then 0
as an output.
01:46
And it’s just in one line because I put it into one print()
call. You can also do it like that, let’s do this to get the exact same output.
01:57
There you see, you get 5
for "Mahdi"
because he’s present in the dictionary, so it gets the value at the key. And because "Jennifer"
isn’t present in the dictionary, it returns you the default value.
02:09 So, this is a good way of getting values from a dictionary where you’re not sure whether the key exists or not, and it’s also a good reminder that if there is a built-in method or function in Python that does what you want it to do, then it’s more idiomatic to use that method rather than writing some complicated logic to achieve the same thing.
Become a Member to join the conversation.