Group List of Dictionary Data by Particular Key in Python
Last Updated :
15 Jul, 2025
Our task is to group the data based on a particular key across these dictionaries. This involves collecting all values associated with a specific key into a single group. For example, if we have a list of dictionaries like this: [{'id': 1, 'name': 'Alice', 'age': 25}, {'id': 2, 'name': 'Bob', 'age': 30}, {'id': 1, 'name': 'Charlie', 'age': 35}] and we want to group them by the 'id' key then the output will be {1: [{'id': 1, 'name': 'Alice', 'age': 25}, {'id': 1, 'name': 'Charlie', 'age': 35}], 2: [{'id': 2, 'name': 'Bob', 'age': 30}]}.
Using a defaultdict from the collections module
defaultdict allows us to automatically create an empty list for any key that doesn't exist and this makes it easier to group data without manually checking if the key exists.
Python
from collections import defaultdict
data = [{'id': 1, 'name': 'Aryan', 'age': 25}, {'id': 2, 'name': 'Harsh', 'age': 30}, {'id': 1, 'name': 'Kunal', 'age': 35}]
res = defaultdict(list)
for item in data:
res[item['id']].append(item)
print(dict(res))
Output{1: [{'id': 1, 'name': 'Aryan', 'age': 25}, {'id': 1, 'name': 'Kunal', 'age': 35}], 2: [{'id': 2, 'name': 'Harsh', 'age': 30}]}
Using itertools.groupby
itertools.groupby can be used to group elements in a sorted list but it requires the list to be sorted by the key and that is why we sort the data first.
Python
from itertools import groupby
data = [{'id': 1, 'name': 'Aryan', 'age': 25}, {'id': 2, 'name': 'Harsh', 'age': 30}, {'id': 1, 'name': 'Kunal', 'age': 35}]
# Sorting the data by the 'id' key
data.sort(key=lambda x: x['id'])
# Using groupby to group the data by 'id'
res = {key: list(group) for key, group in groupby(data, key=lambda x: x['id'])}
print(res)
Output{1: [{'id': 1, 'name': 'Aryan', 'age': 25}, {'id': 1, 'name': 'Kunal', 'age': 35}], 2: [{'id': 2, 'name': 'Harsh', 'age': 30}]}
Explanation:
data.sort(key=lambda x: x['id']) sorts the list of dictionaries based on the 'id' key.groupby(data, key=lambda x: x['id']): then is used to group the sorted data by the 'id' key.
Using a Regular Dictionary with a Loop
In this method we manually create a dictionary and append the dictionaries to the list of the corresponding key.
Python
data = [{'id': 1, 'name': 'Aryan', 'age': 25}, {'id': 2, 'name': 'Harsh', 'age': 30}, {'id': 1, 'name': 'Kunal', 'age': 35}]
res = {}
for item in data:
if item['id'] not in res:
res[item['id']] = []
res[item['id']].append(item)
print(res)
Output{1: [{'id': 1, 'name': 'Aryan', 'age': 25}, {'id': 1, 'name': 'Kunal', 'age': 35}], 2: [{'id': 2, 'name': 'Harsh', 'age': 30}]}
Explanation: for each item in data we check if its 'id' exists as a key in res and if not then we initialize it with an empty list, we then append each dictionary to the list corresponding to its 'id'.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice