PYTHON
Group and Aggregate Data with Python's `defaultdict`
Learn how to efficiently group items and perform aggregations from a list of dictionaries using Python's `collections.defaultdict` for cleaner, concise code.
from collections import defaultdict
transactions = [
{"id": 1, "customer_id": "A101", "amount": 100.50, "item": "Laptop"},
{"id": 2, "customer_id": "B202", "amount": 50.00, "item": "Mouse"},
{"id": 3, "customer_id": "A101", "amount": 75.25, "item": "Keyboard"},
{"id": 4, "customer_id": "C303", "amount": 200.00, "item": "Monitor"},
{"id": 5, "customer_id": "B202", "amount": 25.00, "item": "Mouse Pad"},
]
# Group transactions by customer_id and calculate total amount
customer_summary = defaultdict(lambda: {"total_amount": 0, "transactions": []})
for transaction in transactions:
customer_id = transaction["customer_id"]
customer_summary[customer_id]["total_amount"] += transaction["amount"]
customer_summary[customer_id]["transactions"].append(transaction)
# Convert defaultdict to regular dict for output if needed
final_summary = dict(customer_summary)
print(final_summary)
How it works: This snippet demonstrates how to use `collections.defaultdict` to group data and perform aggregations efficiently. When a key is accessed for the first time, `defaultdict` automatically initializes it with the result of the provided factory function (here, a dictionary with `total_amount` and an empty list). This avoids boilerplate `if key not in dict` checks, making the code cleaner and more readable for common grouping and aggregation tasks, like summarizing customer transactions.