How to Group Data by Key in Python

Group a list of dictionaries by a specified key using a defaultdict and compute per-group averages.

Easy Python 3.9+ Aug 9, 2026 Data pipelines & processing 15 views 0 copies

Python code

23 lines
Python 3.9+
from collections import defaultdict

def group_by_key(data, key):
    grouped = defaultdict(list)
    for item in data:
        grouped[item[key]].append(item)
    return dict(grouped)

if __name__ == "__main__":
    records = [
        {"name": "Alice", "dept": "Engineering", "score": 85},
        {"name": "Bob", "dept": "Sales", "score": 72},
        {"name": "Charlie", "dept": "Engineering", "score": 91},
        {"name": "Diana", "dept": "Sales", "score": 64},
        {"name": "Eve", "dept": "Marketing", "score": 78},
    ]

    by_dept = group_by_key(records, "dept")

    for dept, members in by_dept.items():
        names = ", ".join(m["name"] for m in members)
        avg_score = sum(m["score"] for m in members) / len(members)
        print(f"{dept}: {names} (avg score: {avg_score:.1f})")

Output

stdout
Engineering: Alice, Charlie (avg score: 88.0)
Sales: Bob, Diana (avg score: 68.0)
Marketing: Eve (avg score: 78.0)

How it works

The group_by_key function iterates over each dictionary and uses a defaultdict(list) to automatically create a list for each new key. Appending each item to the list groups records without needing to check if the key exists. Converting back to a regular dict at the end returns a clean dictionary of lists. The main block then iterates over each group, joins member names, and calculates the average score, demonstrating a common ETL pattern.

Common mistakes

  • Using a regular dict and forgetting to initialize empty lists, causing KeyError
  • Assuming all items have the grouping key, leading to KeyError for missing keys
  • Not converting defaultdict back to dict before returning, exposing unexpected behavior

Variations

  1. Use pandas' groupby for larger datasets, e.g., pd.DataFrame(records).groupby('dept').mean()
  2. Use itertools.groupby after sorting the data by the key

Real-world use cases

  • Grouping event logs by user ID to compute session durations in analytics pipelines.
  • Categorizing support tickets by department to aggregate response times and priorities.
  • Batching orders by warehouse region for inventory and shipping processing.

Sponsored

Run this sample

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

Open editor

More from Data pipelines & processing

Related tutorials and quizzes for this topic.