PYTHON
Group Data by Key Using collections.defaultdict
Streamline data grouping in Python by leveraging collections.defaultdict, eliminating verbose conditional checks for dictionary key existence when aggregating data.
from collections import defaultdict
def group_items_by_category(items):
"""
Groups a list of items (dictionaries) by their 'category' key
using defaultdict.
"""
if not isinstance(items, list):
return {}
grouped_data = defaultdict(list)
for item in items:
if isinstance(item, dict) and 'category' in item:
grouped_data[item['category']].append(item)
# Optionally handle items without 'category' or non-dict items
# else:
# print(f"Skipping invalid item: {item}")
return dict(grouped_data) # Convert back to a regular dict if defaultdict behavior is not needed later
# Example usage:
products = [
{'id': 1, 'name': 'Laptop', 'category': 'Electronics', 'price': 1200},
{'id': 2, 'name': 'Mouse', 'category': 'Electronics', 'price': 25},
{'id': 3, 'name': 'Keyboard', 'category': 'Electronics', 'price': 75},
{'id': 4, 'name': 'T-Shirt', 'category': 'Apparel', 'price': 20},
{'id': 5, 'name': 'Jeans', 'category': 'Apparel', 'price': 60},
{'id': 6, 'name': 'Book', 'category': 'Books', 'price': 15}
]
grouped_products = group_items_by_category(products)
# print(f"Grouped Products: {grouped_products}")
# Expected output structure:
# {
# 'Electronics': [...],
# 'Apparel': [...],
# 'Books': [...]
# }
How it works: This snippet showcases `collections.defaultdict` for elegantly grouping data based on a specific key. Instead of checking if a key exists in a dictionary before appending to its list (e.g., `if key not in d: d[key] = []`), `defaultdict(list)` automatically initializes a new list for a key the first time it's accessed. This simplifies the code, making it cleaner and less error-prone when performing common data aggregation tasks like grouping items by category or counting occurrences. The final `dict(grouped_data)` converts the `defaultdict` back to a standard dictionary if preferred.