PYTHON
Simplify Dictionary Initializations with `defaultdict`
Discover `collections.defaultdict` to automatically initialize dictionary values with a default factory, perfect for grouping data, counting occurrences, or building nested structures without boilerplate checks.
from collections import defaultdict
# Grouping items by category
data_points = [
("fruit", "apple"),
("vegetable", "carrot"),
("fruit", "banana"),
("vegetable", "spinach"),
("dairy", "milk")
]
grouped_data = defaultdict(list)
for category, item in data_points:
grouped_data[category].append(item)
print(f"Grouped data: {dict(grouped_data)}")
# Counting occurrences
words = ["apple", "banana", "apple", "orange", "banana", "apple"]
word_counts = defaultdict(int)
for word in words:
word_counts[word] += 1
print(f"Word counts: {dict(word_counts)}")
# Nested defaultdict
nested_data = defaultdict(lambda: defaultdict(list))
# Or, for more complex nesting: nested_data = defaultdict(lambda: defaultdict(lambda: defaultdict(int)))
nested_data["user1"]["posts"].append("Hello world")
nested_data["user1"]["posts"].append("My second post")
nested_data["user2"]["comments"].append("Great post!")
print(f"Nested data: {dict(nested_data)}")
How it works: `collections.defaultdict` is a subclass of `dict` that overrides one method: `__missing__`. When a key is not found, it calls a factory function (provided during instantiation) to create a default value for that key, inserts it, and returns it. This eliminates the need for `if key not in dict:` checks, making code cleaner and more concise for tasks like grouping items into lists or counting occurrences.