Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Handling Missing Keys With defaultdict (Summary)

The Python defaultdict type is a dictionary-like data structure provided by the Python standard library in a module called collections. The class inherits from dict, and its main added functionality is to supply default values for missing keys. In this course, you’ve learned how to use the Python defaultdict type for handling the missing keys in a dictionary.

You’re now able to:

  • Create and use a Python defaultdict to handle missing keys
  • Solve real-world problems related to grouping, counting, and accumulating operations
  • Know the implementation differences between defaultdict and dict
  • Decide when and why to use a Python defaultdict rather than a standard dict

Sample Code (.zip)

2.4 KB

Course Slides (.pdf)

180.8 KB

00:00 Welcome to the seventh and final lesson. This is the concluding lesson and it’s basically a recap of the things which I’ve been showing you in this course.

00:10 We started off in the beginning by introducing defaultdict and I mentioned that it’s a subclass of dict. So it’s really a kind of dictionary which handles missing keys differently from the way dictionaries normally do.

00:22 And it does this by generating a default value whenever you try to access a key which isn’t present in your defaultdict, whereas what would normally happen with a dictionary is you would get an error. You would get a traceback and a KeyError.

00:37 In the second lesson, before really diving into defaultdict and as a way to set the stage more generally, I mentioned the tools which you can use in dict to handle missing keys out of the box. And you really have two of them.

00:49 One is .get(), which returns a value which isn’t mapped to anything, and it doesn’t update the dictionary. What I mean by that is that after the line which ran .get() on your dictionary, your dictionary still doesn’t have an entry for the missing key. Another method which is available to you is .setdefault(). .setdefault() also avoids a traceback but unlike .get(), it also modifies the dictionary.

01:15 So in both of these methods, you’re providing the key which you’re looking for and you’re providing a default value. If the key which you’re looking for is present in the dictionary, then the value mapped to that key will be returned.

01:27 But if the key isn’t in the dictionary, then both of these methods will return a default value, which you’re providing as a second argument. The difference between them is that in the case of .get(), the dictionary is left alone, so after running .get() the dictionary still doesn’t have a value mapped to this key, whereas .setdefault() updates the dictionary. It actually inserts the key with a default value which you provided. In the lesson after that, we really started going into defaultdict.

01:57 We saw that two very important aspects of defaultdict is that, first, it overrides .__missing__() as it commonly exists in dict structures.

02:06 And the way it overrides it is by triggering .default_factory(), and .default_factory() is what is going to generate a default value for your missing key.

02:16 After having started to explore how defaultdict works, we looked at different use cases and how you can use it to solve some very concrete problems. You can use it to group items, to group unique items, to count items, or to accumulate values.

02:30 And the way you adapt defaultdict to these different scenarios is by adjusting which callable you’re going to pass it as an argument. So depending on your use case, you would pass a list, a set, an int, or a float.

02:44 Next, we dived a bit deeper into how defaultdict works under the hood. First of all, we contrasted it with dict. dict is the parent class, so we highlighted which things are different about defaultdict.

02:55 We looked a bit more closely at .default_factorywhich is an attribute of defaultdict—and how you can update it. We contrasted the behavior and some performance issues with .setdefault(), so with one of the standard dict methods for handling missing keys.

03:11 And then finally, I told you a bit more about the role which .__missing__() is playing in the way defaultdict works. Then in the lesson just before this one, I told you about ways in which you can pass arguments to defaultdict.

03:24 So, defaultdict doesn’t really take arguments by design. It’s intended to take a callable as an argument, and that callable doesn’t in turn take any arguments. However, you can use lambda and functools.partial() as ways to get around that limitation and pass arguments to defaultdict when you’re using it. Okay, so that’s it for this course. We covered a lot of ground.

03:48 I hope you enjoyed it as much as I did, and I hope you’ll practice what you’ve learned. Bye-bye!

Ghani on Nov. 22, 2020

Very good course; thanks!

fowydn on June 2, 2023

Very clear tutorial, thank you a lot.

Become a Member to join the conversation.