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.

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

Python code

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

stdout
{'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

  1. Use a regular dict with setdefault: grouped.setdefault(row[key], []).append(row[value_key])
  2. 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

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.