How to Group Rows by Key into Nested Arrays in Python
This code groups rows in a list of dictionaries by a specified key and returns a dictionary with each key mapped to a list of values from another key.
Python code
20 linesfrom collections import defaultdict
def implode_rows(rows, key, value_key):
grouped = defaultdict(list)
for row in rows:
grouped[row[key]].append(row[value_key])
return dict(grouped)
if __name__ == "__main__":
data = [
{"category": "fruit", "item": "apple"},
{"category": "fruit", "item": "banana"},
{"category": "veg", "item": "carrot"},
{"category": "fruit", "item": "cherry"},
{"category": "veg", "item": "broccoli"},
]
result = implode_rows(data, "category", "item")
print(result)
Output
{'fruit': ['apple', 'banana', 'cherry'], 'veg': ['carrot', 'broccoli']}
How it works
The defaultdict from the collections module provides a default factory (here, list) so that appending to a non-existent key does not raise a KeyError. For each row, the value associated with value_key is appended to the list for the group identified by row[key]. Converting the defaultdict to a regular dict at the end gives a cleaner representation and avoids unexpected default behavior when accessing non-existent keys. This pattern is a classic data aggregation transform, similar to a GROUP BY in SQL but returning a nested array structure.
Common mistakes
- Forgetting to convert the defaultdict to a plain dict at the end, which can lead to default values on access.
- Assuming the input rows are already sorted, which is unnecessary since grouping handles any order.
- Using a key that may be missing in some rows, causing a KeyError; ensure the key exists or use .get().
Variations
- Use a regular dict with setdefault: grouped.setdefault(row[key], []).append(row[value_key])
- Use itertools.groupby after sorting by the key for a streaming approach.
Real-world use cases
- Transforming query results into a nested structure for API responses where customers have many orders.
- Aggregating log entries by service name to display counts or lists in a dashboard.
- Grouping sensor readings by device ID before feeding into a time-series analysis pipeline.
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.