Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is defaultdict in Python

Understanding defaultdict in Python

When you're just starting out with programming, managing and organizing data efficiently is a skill that you'll find invaluable. Python, with its simplicity and readability, offers a variety of tools to help you do just that. One such tool is the defaultdict, a type of dictionary that's part of the collections module.

What is a Dictionary?

Before diving into defaultdict, let's first understand what a regular dictionary in Python is. Imagine a real-life dictionary that maps words to their meanings. Similarly, a Python dictionary maps 'keys' to 'values'. It's like a basket where every item has a label, and you can quickly find any item by just looking for its label.

Here's a simple example of a dictionary:

fruit_colors = {
    'apple': 'red',
    'banana': 'yellow',
    'grape': 'purple'

In this fruit_colors dictionary, the names of the fruits are the keys, and the colors are the values.

The Problem with Regular Dictionaries

What happens when you try to access a key that doesn't exist in a regular dictionary? You get an error! For example:

print(fruit_colors['orange'])  # This will raise a KeyError

This behavior can be problematic when you're not sure if a key exists and you want to avoid errors. You could check if the key exists before accessing it, but that can make your code longer and harder to read.

Enter defaultdict

The defaultdict works around this issue by providing a default value for keys that don't exist. When you try to access or modify a key that's not present in the dictionary, defaultdict automatically creates the key and assigns a default value to it. This default value is determined by a function that you provide when you create the defaultdict.

Creating a defaultdict

To use defaultdict, you first need to import it from the collections module:

from collections import defaultdict

Then, you create a defaultdict by providing a function that returns the default value. This function is often referred to as the 'default factory'. Here's an example:

fruit_counts = defaultdict(int)  # int() returns 0

In this case, int is our default factory, and it returns 0. So, if we try to access a key that doesn't exist, 0 will be automatically assigned to it.

Examples of defaultdict in Action

Let's see defaultdict in action with some code examples. Suppose you're counting the number of times each letter appears in a word:

word = 'mississippi'
letter_counts = defaultdict(int)  # Default value of int is 0

for letter in word:
    letter_counts[letter] += 1

print(letter_counts)  # Output: defaultdict(<class 'int'>, {'m': 1, 'i': 4, 's': 4, 'p': 2})

Without a defaultdict, you would have to write additional code to handle the case where a key isn't already present in the dictionary.

Using Different Default Factories

The default factory can be any callable that returns a value, such as int, list, set, or even a custom function. Here's an example using list as the default factory:

fruit_locations = defaultdict(list)


print(fruit_locations)  # Output: defaultdict(<class 'list'>, {'market': ['apple'], 'home': ['banana']})

In this example, if a location doesn't exist when we try to append a fruit to it, a new list is created automatically.

defaultdict vs. setdefault Method

You might be wondering how defaultdict is different from the setdefault method of a regular dictionary. The setdefault method also provides a default value for keys that don't exist. However, it's not as efficient as defaultdict because it always creates a new instance of the default value even if the key already exists.

Here's how you could use setdefault in our letter counting example:

word = 'mississippi'
letter_counts = {}

for letter in word:
    letter_counts.setdefault(letter, 0)
    letter_counts[letter] += 1

print(letter_counts)  # Output: {'m': 1, 'i': 4, 's': 4, 'p': 2}

Intuitions and Analogies

Think of defaultdict as a smart vending machine that never runs out of any item. If you select an item that's out of stock, the machine magically produces it for you on the spot. The 'default factory' is like the recipe the machine uses to create the item.


The defaultdict in Python is a powerful tool that can make your code cleaner and more efficient. It's particularly useful when you're dealing with data that might have missing elements and you want to avoid errors or extra code to handle those cases.

As a beginner, mastering tools like defaultdict will help you tackle more complex problems and write code that's not just functional, but elegant and Pythonic. Remember, the best way to get comfortable with these concepts is to practice. Try creating your own defaultdicts with different default factories and see how they behave. Happy coding!