PYTHON
Grouping Data by Key with `defaultdict`
Learn to efficiently group data points into lists based on a common key using Python's `collections.defaultdict`, perfect for categorizing records.
from collections import defaultdict
data = [
{'category': 'fruit', 'item': 'apple'},
{'category': 'vegetable', 'item': 'carrot'},
{'category': 'fruit', 'item': 'banana'},
{'category': 'dairy', 'item': 'milk'},
{'category': 'vegetable', 'item': 'broccoli'},
]
grouped_data = defaultdict(list)
for record in data:
grouped_data[record['category']].append(record['item'])
# Convert defaultdict to a regular dict for final output if needed
final_result = dict(grouped_data)
print(final_result)
# Expected: {'fruit': ['apple', 'banana'], 'vegetable': ['carrot', 'broccoli'], 'dairy': ['milk']}
How it works: This snippet demonstrates using `collections.defaultdict(list)` to group items efficiently. When a key is accessed for the first time, `defaultdict` automatically creates an empty list for that key, allowing you to directly append items without checking if the key already exists. This simplifies code and prevents `KeyError` exceptions.