PYTHON

Streamline Dictionary Value Assignment with `collections.defaultdict`

Simplify code and prevent `KeyError` by using `collections.defaultdict` in Python. Automatically initialize values for new keys with a default factory function.

from collections import defaultdict

# Example 1: Grouping items into lists
# If a key is accessed for the first time, a new empty list is created.
grouped_data = defaultdict(list)
items = [('fruit', 'apple'), ('vegetable', 'carrot'), ('fruit', 'banana'), ('vegetable', 'broccoli')]

for category, item in items:
    grouped_data[category].append(item)

print(f"Grouped data by category: {grouped_data}")
# Expected: defaultdict(<class 'list'>, {'fruit': ['apple', 'banana'], 'vegetable': ['carrot', 'broccoli']})

# Example 2: Counting occurrences (alternative to Counter for simple cases)
word_counts = defaultdict(int)
sentence = "this is a test this is only a test"
words = sentence.split()

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

print(f"Word counts: {word_counts}")
# Expected: defaultdict(<class 'int'>, {'this': 2, 'is': 2, 'a': 2, 'test': 2, 'only': 1})

# Accessing a non-existent key will create it with the default value
print(f"Accessing non-existent key 'animal': {grouped_data['animal']}")
print(f"Grouped data after accessing 'animal': {grouped_data}")
How it works: The `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 `default_factory` (the argument passed during initialization) to create a default value for that key, inserts it, and then returns it. This eliminates the need for `if key not in dict:` checks and simplifies code for common patterns like grouping items or counting occurrences.

Need help integrating this into your project?

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

Hire DigitalCodeLabs