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.

Easy Python 3.9+ Aug 9, 2026 Testing & modern typing 12 views 0 copies

Python code

32 lines
Python 3.9+
from 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

stdout
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

  1. Use `collections.defaultdict(list)` and append items to the default list for slightly cleaner code
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Testing & modern typing

Related tutorials and quizzes for this topic.