Handling Missing Keys

00:00 Welcome back! In the introductory lesson, I mentioned that what sets defaultdict and dict apart from each other is mainly the way in which they handle missing keys. And also in the introductory lesson, I showed you how you use keys to assign values to dictionaries and how to retrieve them. In this lesson, we’ll go into how dictionaries handle missing keys a bit more deeply.

00:21 Let’s jump right back into the REPL,

00:23 and this is where we left off in the last lesson. I had tried to look up the key 'b', which doesn’t exist, and that generated a traceback. I got a KeyError.

00:34 So, there are two ways in which dictionaries allow you to avoid KeyError right out of the box. What sets them apart from each other is whether or not they modify the dictionary. Let me show you what I mean.

00:45 I got this last KeyError because I looked up the value for 'b' and there is no 'b' key. However, if I use .get() like this and enter as a first parameter the key which I’m looking for and as a second parameter a default value, then my dictionary returns 2. It returns 2 even though there is no entry for 'b'. In other words, it behaves as if an entry existed for this key, and it doesn’t error out.

01:15 But an important thing to keep in mind is that if I look at my dictionary,

01:21 I see that there’s still only one entry. There’s only a value for the 'a' key. In other words, I didn’t get an error, but I also didn’t update my dictionary.

01:30 If I did want to update my dictionary, I would use a different method and that’s .setdefault(). Let me show you how that works. So, I’ll use .setdefault(), and again, I’m entering two parameters.

01:43 The first is the key which I’m looking for, and then the second is a default value. And so far, this looks just like .get(). After all, I didn’t get an error even though there was no entry for 'b', and my line of code returned the value 2 as if that key had been present in the dictionary.

02:02 But watch what happens when I look into my dictionary. In this case, the dictionary has been modified. It’s been updated, and this value has been entered into the dictionary.

02:15 Just before we move on here, let me show you what happens if I use .get() to look for this key but with a different value. I still get a 2.

02:24 This is because since there’s already an entry for 'b', neither of these methods will override what’s already in there.

02:33 So to sum up, there are two methods which allow dictionaries to handle missing keys gracefully—gracefully in the sense that they won’t error out. The first is .get(), which you pass with a key you’re looking for and a value which you would like to have returned if there is no value actually assigned to that key already.

02:51 The second is .setdefault(). It does pretty much the same thing as .get() except it creates this key in the dictionary, and it creates an entry—so it enters the value which you provide. It doesn’t only return it to you. In both of these cases, if the key which you were looking for already existed in the dictionary, then you would just have gotten the value which was already there.

03:12 The default values which you entered would just be ignored. Okay, so that’s what dictionaries can do right out of the box.

03:20 defaultdict is sort of an expanded dictionary, or a dictionary with super powers. In fact, as we’ll see, it inherits from dict (dictionary).

03:27 But we’ll go into that in the next lesson. I’ll see you there.

Become a Member to join the conversation.