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.
Python code
23 linesfrom 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
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
- Use pandas' groupby for larger datasets, e.g., pd.DataFrame(records).groupby('dept').mean()
- 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
More from Data pipelines & processing
- Add a UUID Surrogate Key to Each Row in a CSV with Python easy
- Attach Source File Metadata to Records in Python easy
- Build a Python Utility That Detects Duplicate Records Across Multiple Excel Sheets medium
- Check Null Rate Threshold in PySpark DataFrame medium
- Count Records Processed per Category in Python easy
- Create Data Helper Functions in Python for Beginners easy
Keep learning
Related tutorials and quizzes for this topic.