← Back to all snippets
PYTHON

Grouping Data with collections.defaultdict

Learn to efficiently group items by a key into lists or other data structures using Python's `collections.defaultdict`, perfect for processing API data.

from collections import defaultdict

def group_items_by_category(items):
    grouped_data = defaultdict(list)
    for item in items:
        grouped_data[item['category']].append(item['name'])
    return dict(grouped_data)

# Example usage with web data
product_data = [
    {'name': 'Laptop Pro', 'category': 'Electronics', 'price': 1200},
    {'name': 'Gaming Mouse', 'category': 'Electronics', 'price': 75},
    {'name': 'Office Chair', 'category': 'Furniture', 'price': 300},
    {'name': 'Desk Lamp', 'category': 'Furniture', 'price': 50},
    {'name': 'Smartwatch X', 'category': 'Wearables', 'price': 250},
]

grouped_products = group_items_by_category(product_data)
# Expected: {'Electronics': ['Laptop Pro', 'Gaming Mouse'], 'Furniture': ['Office Chair', 'Desk Lamp'], 'Wearables': ['Smartwatch X']}
print(grouped_products)

# Using defaultdict with a different factory (e.g., int for counts)
event_logs = [
    {'user': 'alice', 'action': 'login'},
    {'user': 'bob', 'action': 'view_product'},
    {'user': 'alice', 'action': 'add_to_cart'},
    {'user': 'charlie', 'action': 'login'},
    {'user': 'bob', 'action': 'purchase'},
]

action_counts = defaultdict(int)
for log in event_logs:
    action_counts[log['action']] += 1

print(dict(action_counts))
# Expected: {'login': 2, 'view_product': 1, 'add_to_cart': 1, 'purchase': 1}
How it works: The `collections.defaultdict` is a dictionary subclass that calls a factory function to supply missing values. When you try to access a key that isn't yet in the dictionary, `defaultdict` automatically creates an entry using the provided `default_factory` (e.g., `list` or `int`), allowing you to append to lists or increment counts without explicit `if key not in dict` checks. This simplifies grouping and aggregation tasks common in web development, such as organizing API responses or user activity logs.

Need help integrating this into your project?

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

Hire DigitalCodeLabs