0
0
PythonHow-ToBeginner · 3 min read

How to Group List of Dictionaries by Key in Python

To group a list of dictionaries by a key in Python, use itertools.groupby after sorting the list by that key, or use collections.defaultdict to collect items under each key. Both methods organize dictionaries sharing the same key value together.
📐

Syntax

There are two common ways to group dictionaries by a key:

  • Using itertools.groupby: Sort the list by the key, then group.
  • Using collections.defaultdict: Create a dictionary where each key maps to a list of dictionaries.
python
from itertools import groupby
from operator import itemgetter
from collections import defaultdict

# Using groupby
sorted_list = sorted(list_of_dicts, key=itemgetter('key_name'))
groups = {k: list(v) for k, v in groupby(sorted_list, key=itemgetter('key_name'))}

# Using defaultdict
groups = defaultdict(list)
for d in list_of_dicts:
    groups[d['key_name']].append(d)
💻

Example

This example shows how to group a list of dictionaries by the key category using both groupby and defaultdict.

python
from itertools import groupby
from operator import itemgetter
from collections import defaultdict

items = [
    {'name': 'apple', 'category': 'fruit'},
    {'name': 'carrot', 'category': 'vegetable'},
    {'name': 'banana', 'category': 'fruit'},
    {'name': 'celery', 'category': 'vegetable'},
    {'name': 'pear', 'category': 'fruit'}
]

# Using groupby
items_sorted = sorted(items, key=itemgetter('category'))
grouped_by_category = {k: list(v) for k, v in groupby(items_sorted, key=itemgetter('category'))}
print('Using groupby:')
for category, group in grouped_by_category.items():
    print(f'{category}: {group}')

# Using defaultdict
groups = defaultdict(list)
for item in items:
    groups[item['category']].append(item)

print('\nUsing defaultdict:')
for category, group in groups.items():
    print(f'{category}: {group}')
Output
Using groupby: fruit: [{'name': 'apple', 'category': 'fruit'}, {'name': 'banana', 'category': 'fruit'}, {'name': 'pear', 'category': 'fruit'}] vegetable: [{'name': 'carrot', 'category': 'vegetable'}, {'name': 'celery', 'category': 'vegetable'}] Using defaultdict: fruit: [{'name': 'apple', 'category': 'fruit'}, {'name': 'banana', 'category': 'fruit'}, {'name': 'pear', 'category': 'fruit'}] vegetable: [{'name': 'carrot', 'category': 'vegetable'}, {'name': 'celery', 'category': 'vegetable'}]
⚠️

Common Pitfalls

Sorting is required for groupby to work correctly. If the list is not sorted by the grouping key, groups will be incorrect or incomplete.

Using defaultdict does not require sorting and is often simpler.

python
from itertools import groupby
from operator import itemgetter

items = [
    {'name': 'apple', 'category': 'fruit'},
    {'name': 'carrot', 'category': 'vegetable'},
    {'name': 'banana', 'category': 'fruit'}
]

# Wrong: groupby without sorting
groups_wrong = {k: list(v) for k, v in groupby(items, key=itemgetter('category'))}
print('Wrong groupby result:', groups_wrong)

# Right: sort before groupby
items_sorted = sorted(items, key=itemgetter('category'))
groups_right = {k: list(v) for k, v in groupby(items_sorted, key=itemgetter('category'))}
print('Correct groupby result:', groups_right)
Output
Wrong groupby result: {'fruit': [{'name': 'apple', 'category': 'fruit'}], 'vegetable': [{'name': 'carrot', 'category': 'vegetable'}], 'fruit': [{'name': 'banana', 'category': 'fruit'}]} Correct groupby result: {'fruit': [{'name': 'apple', 'category': 'fruit'}, {'name': 'banana', 'category': 'fruit'}], 'vegetable': [{'name': 'carrot', 'category': 'vegetable'}]}
📊

Quick Reference

Use this quick guide to choose your grouping method:

MethodRequires SortingUse When
itertools.groupbyYesYou want a memory-efficient iterator and sorted data
collections.defaultdictNoYou want simple code and unordered grouping
MethodRequires SortingUse When
itertools.groupbyYesYou want a memory-efficient iterator and sorted data
collections.defaultdictNoYou want simple code and unordered grouping

Key Takeaways

Use itertools.groupby after sorting the list by the key to group dictionaries efficiently.
collections.defaultdict allows grouping without sorting and is simpler for most cases.
Always sort your list before using groupby to avoid incorrect grouping.
defaultdict groups items in insertion order and is easier for beginners.
Choose groupby for memory efficiency and defaultdict for simplicity.