How to Group Data by Key in Python with Type Hints
Group a list of dictionaries by a specified key using a typed helper function and print a summary of each group.
Python code
32 linesfrom typing import Any, Dict, List, TypeVar, Union
T = TypeVar("T")
def group_by(data: List[Dict[str, Any]], key: str) -> Dict[Any, List[Dict[str, Any]]]:
"""Group a list of dictionaries by a given key."""
grouped: Dict[Any, List[Dict[str, Any]]] = {}
for item in data:
value = item.get(key)
grouped.setdefault(value, []).append(item)
return grouped
def summarize(grouped: Dict[Any, List[Dict[str, Any]]]) -> None:
"""Print a summary of grouped data."""
for group_key, items in grouped.items():
print(f"{group_key}: {len(items)} item(s) -> {items}")
if __name__ == "__main__":
people = [
{"name": "Alice", "age": 30, "city": "NYC"},
{"name": "Bob", "age": 25, "city": "LA"},
{"name": "Charlie", "age": 30, "city": "NYC"},
{"name": "Diana", "age": 25, "city": "LA"},
]
by_city = group_by(people, "city")
summarize(by_city)
print()
by_age = group_by(people, "age")
summarize(by_age)
Output
NYC: 2 item(s) -> [{'name': 'Alice', 'age': 30, 'city': 'NYC'}, {'name': 'Charlie', 'age': 30, 'city': 'NYC'}]
LA: 2 item(s) -> [{'name': 'Bob', 'age': 25, 'city': 'LA'}, {'name': 'Diana', 'age': 25, 'city': 'LA'}]
30: 2 item(s) -> [{'name': 'Alice', 'age': 30, 'city': 'NYC'}, {'name': 'Charlie', 'age': 30, 'city': 'NYC'}]
25: 2 item(s) -> [{'name': 'Bob', 'age': 25, 'city': 'LA'}, {'name': 'Diana', 'age': 25, 'city': 'LA'}]
How it works
The group_by function iterates over each dictionary and uses setdefault to create a new list for new keys or append to an existing list. Using TypeVar in the return type signature makes the function more flexible — the grouping key can be any hashable type (str, int, etc.). The if __name__ == "__main__" guard ensures the demo code runs only when executed directly, not when imported as a module.
Common mistakes
- Forgetting `.get(key)` and getting a KeyError crash when a dict lacks the key
- Using `defaultdict` without importing it when the standard library is sufficient
- Mutating lists after grouping and accidentally modifying the original `data`
Variations
- Use `collections.defaultdict(list)` and append items to the default list for slightly cleaner code
- Group more complex nested data with multiple keys by passing a tuple key
Real-world use cases
- Grouping log entries by severity level to visualize error counts in analytics dashboards.
- Batching database records by customer ID to run per-user ETL or aggregation jobs.
- Organizing API response items by category in a data-processing pipeline before serialization.
Sponsored
More from Testing & modern typing
- Capture stdout and stderr with pytest capsys easy
- Characterization Test for Legacy Python Code medium
- Dataclass with Type Hints Fields in Python easy
- Dependency Injection in Python for Testability easy
- Design Data Helpers with Python TypedDict and Literal easy
- Fix and Test a Regression Bug in Python with Unit Tests easy
Keep learning
Related tutorials and quizzes for this topic.