← Back to all snippets
PYTHON

Simplifying Dictionary Initialization with collections.defaultdict

Avoid KeyError and streamline dictionary value initialization using Python's collections.defaultdict, perfect for grouping data or counting occurrences.

from collections import defaultdict

# Use defaultdict with a list factory for grouping
grouped_data = defaultdict(list)
data_points = [('apple', 1), ('banana', 2), ('apple', 3), ('orange', 4), ('banana', 5)]

for key, value in data_points:
    grouped_data[key].append(value)

print(f"Grouped data (list): {grouped_data}")
# Expected: defaultdict(<class 'list'>, {'apple': [1, 3], 'banana': [2, 5], 'orange': [4]})

# Use defaultdict with an int factory for counting
word_counts = defaultdict(int)
sentence = "the quick brown fox jumps over the lazy dog quick brown"
words = sentence.split()

for word in words:
    word_counts[word] += 1

print(f"Word counts (int): {word_counts}")
# Expected: defaultdict(<class 'int'>, {'the': 2, 'quick': 2, 'brown': 2, 'fox': 1, 'jumps': 1, 'over': 1, 'lazy': 1, 'dog': 1})

# Accessing a non-existent key won't raise KeyError, it creates it with default value
print(f"Accessing non-existent key 'grape': {grouped_data['grape']}")
print(f"Grouped data after accessing 'grape': {grouped_data}")
How it works: collections.defaultdict is a subclass of dict that overrides one method: __missing__. When you try to access a key that isn't in the dictionary, defaultdict calls the factory function (provided during initialization, e.g., list, int, set) with no arguments to produce a default value for that key, inserts it, and then returns it. This simplifies code by eliminating explicit checks for key existence, making it ideal for tasks like grouping items or counting frequencies without boilerplate try-except or if-else blocks.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs