PYTHON

Group Data by Key with collections.defaultdict

Learn to efficiently group items by a common key using Python's collections.defaultdict, simplifying data aggregation from lists of dictionaries or objects received from APIs or databases.

from collections import defaultdict

# Sample data: a list of users with their country
users = [
    {"name": "Alice", "country": "USA"},
    {"name": "Bob", "country": "Canada"},
    {"name": "Charlie", "country": "USA"},
    {"name": "David", "country": "UK"},
    {"name": "Eve", "country": "Canada"},
]

# Group users by country
users_by_country = defaultdict(list)
for user in users:
    users_by_country[user["country"]].append(user["name"])

print(f"Users grouped by country: {dict(users_by_country)}")
# Output: Users grouped by country: {'USA': ['Alice', 'Charlie'], 'Canada': ['Bob', 'Eve'], 'UK': ['David']}

# Example with counting occurrences
event_logs = ["login", "logout", "login", "view_profile", "login", "view_profile"]
event_counts = defaultdict(int)
for event in event_logs:
    event_counts[event] += 1

print(f"Event counts: {dict(event_counts)}")
# Output: Event counts: {'login': 3, 'logout': 1, 'view_profile': 2}
How it works: `collections.defaultdict` is a subclass of `dict` that calls a factory function (e.g., `list` or `int`) to supply missing values when a key is accessed for the first time. This eliminates the need for explicit `if key not in dict:` checks, making code for grouping or accumulating values much cleaner and more concise. It's highly useful when processing data from an API or database where you need to aggregate items based on a common attribute.

Need help integrating this into your project?

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

Hire DigitalCodeLabs