PYTHON
Grouping Items Efficiently with `defaultdict`
Learn how to use Python's `collections.defaultdict` to elegantly group data items based on a common key, perfect for processing API responses or user submissions.
from collections import defaultdict
data = [
{"category": "fruits", "item": "apple"},
{"category": "vegetables", "item": "carrot"},
{"category": "fruits", "item": "banana"},
{"category": "dairy", "item": "milk"},
{"category": "vegetables", "item": "broccoli"},
]
grouped_data = defaultdict(list)
for entry in data:
grouped_data[entry["category"]].append(entry["item"])
print(grouped_data)
# Example output:
# defaultdict(<class 'list'>, {'fruits': ['apple', 'banana'], 'vegetables': ['carrot', 'broccoli'], 'dairy': ['milk']})
How it works: The `defaultdict` from the `collections` module automatically initializes a default value for a new key if it's accessed and not yet present. By specifying `list` as the default factory, you can easily group items. When iterating through `data`, if a category key doesn't exist in `grouped_data`, a new empty list is created for it before the item is appended, simplifying grouping logic compared to checking `if key not in dict: dict[key] = []`.