Handling Missing Keys With the Python defaultdict Type

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 regular dict
  • How to use a defaultdict for grouping, counting, and accumulating operations

What’s Included:

Downloadable Resources:

About Christian Mondorf

Christian Mondorf Christian Mondorf

Christian is an avid Pythonista and video instructor at Real Python.

» More about Christian

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

« Browse All Courses