Handling Missing Keys With defaultdict (Overview)
The Python defaultdict
type behaves almost exactly like a regular Python dictionary, but if you try to access or modify a missing key, then defaultdict
will automatically create the key and generate a default value for it. This makes defaultdict
a valuable option for handling missing keys in dictionaries.
In this course, you’ll learn:
- How to use the Python
defaultdict
type for handling missing keys in a dictionary - When and why to use a Python
defaultdict
rather than a regulardict
- How to use a
defaultdict
for grouping, counting, and accumulating operations
00:00
Hello! Welcome to this course on handling missing keys with defaultdict
in Python. defaultdict
is closely related to dictionaries, so before we really dive in, I’d like to give you a quick reminder about dictionaries in Python.
00:13 Let’s switch over to the REPL. Here I am in my terminal. I’m going to go into Python. Now just as a quick reminder, a dictionary is a mapping of keys to values.
00:25
I’m going to start by creating an empty dictionary here. I’m going to call it a_dict
. I’ll use the assignment operator (=
) and then the syntax to create a completely empty dictionary is to open and close curly brackets, like this. Let’s just run type()
on this, and you can check that this is a dictionary and I’m not making something up. And you can see this belongs to the <class 'dict'>
. Now, this is important—we’ll get back to this later on—but defaultdict
, which is actually the subject of this course, is a subclass of dict
.
00:54 So the two are very closely related to one another.
00:57
When I want to add a value to my dictionary, what I do is I first describe which key I want to add it to. So in this case, I’m going to add a key, which is just the string 'a'
. And then I use the assignment operator (=
) to map a value to this key.
01:13
In this case, I’m going to give it the value 1
.
01:16 So now I can retrieve the value, just referring to this key. The syntax here is the dictionary name, and then the key name in square brackets. If I hit Enter, then I get my value back. So far so good, but sometimes you can have problems—for example, if you’re trying to access a key which doesn’t exist in your dictionary.
01:35
So let’s try modifying my last line of code, except this time I’m going to replace 'a'
with 'b'
, and there is no 'b'
in my dictionary. As you can see this results in a traceback—it gives me a KeyError
.
01:47
And this is bad because it would stop the flow of my code. What defaultdict
really allows us to do is it gives us a way to handle missing keys much better. As I mentioned, defaultdict
is actually a subclass of dict
, so it behaves in a very, very similar way.
02:03 But what it does is when it’s confronted with a key that’s missing in your dictionary, it will create that key and generate a default value for it.
02:12 We’re currently on Lesson 1—this is just the introductory lesson. The next lesson is still about dictionaries. I’ll go over a bit more detail on how you can handle missing keys in dictionaries using just the tools which dictionaries provide you out of the box.
02:25
In Lesson 3, I will help you understand the basics of defaultdict
. After that, in Lesson 4, I’ll show you some examples of how you can use defaultdict
to solve some concrete problems, writing very clear and concise and Pythonic code. With those examples in mind, in Lesson 5 we can dive a bit deeper into defaultdict
and how it’s working behind the scenes.
02:45
In Lesson 6 I’ll show you how to pass arguments to defaultdict
. And then finally, in Lesson 7 we’ll conclude and wrap up.
02:52 The first thing I’m going to show you is how you can handle missing keys in dictionaries using nothing but what dictionaries bring with themselves out of the box.
Become a Member to join the conversation.