PYTHON
Group Items by Key Using `defaultdict`
Learn how to simplify grouping items by a common key into lists or other collections using `collections.defaultdict`, avoiding boilerplate checks.
from collections import defaultdict
data = [
{'name': 'Alice', 'city': 'New York'},
{'name': 'Bob', 'city': 'London'},
{'name': 'Charlie', 'city': 'New York'},
{'name': 'David', 'city': 'London'}
]
# Group by city
grouped_by_city = defaultdict(list)
for item in data:
grouped_by_city[item['city']].append(item['name'])
print(dict(grouped_by_city))
# Output: {'New York': ['Alice', 'Charlie'], 'London': ['Bob', 'David']}
# Example with integer keys and sets
numbers = [1, 2, 3, 4, 5, 6]
parity_groups = defaultdict(set)
for num in numbers:
parity_groups['even' if num % 2 == 0 else 'odd'].add(num)
print(dict(parity_groups))
# Output: {'odd': {1, 3, 5}, 'even': {2, 4, 6}}
How it works: `collections.defaultdict` is a dictionary subclass that streamlines the process of assigning default values to non-existent keys. When you attempt to access a key that isn't present, it automatically invokes the factory function (e.g., `list`, `int`, `set`) provided during its instantiation to create a default value for that key. This pattern significantly simplifies grouping operations, eliminating the need for explicit `if key not in dict:` checks and making code cleaner and more concise.