Welcome back. In the last lesson, we looked at how dictionaries handle missing keys using just what they have for being dictionaries. In this lesson, we’re ready to look a bit more closely at
you can see that here there’s an entry for
collections under Data Types. And if I click on that and follow that link, then there’s an entry here for
defaultdict. This is a
dict subclass—remember, I told you that
defaultdict inherits from
dict—and what’s interesting about it is that it “calls a factory function to supply missing values.” And what they’re referring to, of course, is values which are missing for a certain key when you’re trying to access a key.
In case that was a bit too quick, I clicked on that link and came here to the documentation for
defaultdict. What is interesting about
defaultdict is that if a key isn’t present in the dictionary—or in the
defaultdict, in this case—then that triggers
.__missing__() in turn triggers
.default_factory(), and this is what is going to generate a default value for the key which you were trying to access but wasn’t there.
The first thing I’m going to do here is that from
collections, I’m going to import
defaultdict. There we go. So, this is what you would do anytime that you want to work with
defaultdict: you would load it into memory from a package called
collections. Before we move on, I would just like to prove to you that it is a subclass of
dict, and I’ll do that using
issubclass(). This takes two parameters.
And this returns
True. So you can see I’m not lying.
defaultdict does inherit from
dict. And that means that, for the most part, it behaves just like a normal dictionary. Okay, so let’s create a
defaultdict. We’re to name this one
def_dict and I will use the assignment operator (
=) and then the syntax is
defaultdict()—and here, I have to pass a callable.
So this can be
list, et cetera. I’m going to go with
list for now. Now you’ll notice that I just used the keyword
list—there’s no open and close of parentheses after
list. In fact, that’s a common mistake, so do pay attention to that. Okay.
So now I have a
defaultdict here. We can just check the type quickly and you can see this is a
defaultdict and it comes from
collections. And just like with a dictionary, I can assign values to keys. I’m going to create a first key, I’ll call it
"one", and I’m going to sign the value
1—so just the integer
So, no error; my code continues to work. And I can use this
list. In fact, let’s try something slightly different. Rather than trying to access
"missing", which now isn’t missing, I will try accessing
These are the main takeaways from this lesson. Unlike what happens with a dictionary, which will give you a
KeyError if you try to access a key which isn’t present, a
defaultdict will trigger
.__missing__(), which in turn triggers
The exact behavior of
.default_factory() will depend on what you passed as a parameter when you first constructed your
defaultdict. So in our example, I gave it
list, and so it created a
list whenever it encountered a key that was missing.
Become a Member to join the conversation.