Group List of Dictionary Data by Particular Key in Python

Group List of Dictionary Data by Particular Key in Python can be done using itertools.groupby() method.

Itertools.groupby()

This method calculates the keys for each element present in iterable. It returns key and iterable of grouped items.

Syntax: itertools.groupby(iterable, key_func)

Parameters:

  • iterable: Iterable can be of any kind (list, tuple, dictionary).
  • key_func: A function that calculates keys for each element present in iterable.

Return type: It returns consecutive keys and groups from the iterable. If the key function is not specified or is None, key defaults to an identity function and returns the element unchanged.



Let’s see the examples:
Example 1: Suppose we have list of dictionary of employee and company.

INFO = [
    {'employee': 'XYZ_1', 'company': 'ABC_1'},
    {'employee': 'XYZ_2', 'company': 'ABC_2'},
    {'employee': 'XYZ_3', 'company': 'ABC_3'},
    {'employee': 'XYZ_4', 'company': 'ABC_3'},
    {'employee': 'XYZ_5', 'company': 'ABC_2'},
    {'employee': 'XYZ_6', 'company': 'ABC_3'},
    {'employee': 'XYZ_7', 'company': 'ABC_1'},
    {'employee': 'XYZ_8', 'company': 'ABC_2'},
    {'employee': 'XYZ_9', 'company': 'ABC_1'}
]

Now we need to display all the data group by the ‘company’ key name.

Code:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import a groupby() method
# from itertools module
from itertools import groupby
  
# dictionary
INFO = [
    {'employee': 'XYZ_1', 'company': 'ABC_1'},
    {'employee': 'XYZ_2', 'company': 'ABC_2'},
    {'employee': 'XYZ_3', 'company': 'ABC_3'},
    {'employee': 'XYZ_4', 'company': 'ABC_3'},
    {'employee': 'XYZ_5', 'company': 'ABC_2'},
    {'employee': 'XYZ_6', 'company': 'ABC_3'},
    {'employee': 'XYZ_7', 'company': 'ABC_1'},
    {'employee': 'XYZ_8', 'company': 'ABC_2'},
    {'employee': 'XYZ_9', 'company': 'ABC_1'}
]
  
  
# define a fuction for key
def key_func(k):
    return k['company']
  
# sort INFO data by 'company' key.
INFO = sorted(INFO, key=key_func)
  
for key, value in groupby(INFO, key_func):
    print(key)
    print(list(value))

chevron_right


Output:

ABC_1
[{’employee’: ‘XYZ_1’, ‘company’: ‘ABC_1′}, {’employee’: ‘XYZ_7’, ‘company’: ‘ABC_1′}, {’employee’: ‘XYZ_9’, ‘company’: ‘ABC_1′}]
ABC_2
[{’employee’: ‘XYZ_2’, ‘company’: ‘ABC_2′}, {’employee’: ‘XYZ_5’, ‘company’: ‘ABC_2′}, {’employee’: ‘XYZ_8’, ‘company’: ‘ABC_2′}]
ABC_3
[{’employee’: ‘XYZ_3’, ‘company’: ‘ABC_3′}, {’employee’: ‘XYZ_4’, ‘company’: ‘ABC_3′}, {’employee’: ‘XYZ_6’, ‘company’: ‘ABC_3’}]

Example 2: Suppose we have list of dictionary of student grades and marks.

students = [
    {'mark': '65','grade': 'C'},
    {'mark': '86','grade': 'A'},
    {'mark': '73','grade': 'B'},
    {'mark': '49','grade': 'D'},
    {'mark': '91','grade': 'A'},
    {'mark': '79','grade': 'B'}
]


Now we need to display all the data group by the ‘grade’ key.

Code:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import required methods
from itertools import groupby
from operator import itemgetter
  
# dictionary
students = [
    {'mark': '65', 'grade': 'C'},
    {'mark': '86', 'grade': 'A'},
    {'mark': '73', 'grade': 'B'},
    {'mark': '49', 'grade': 'D'},
    {'mark': '91', 'grade': 'A'},
    {'mark': '79', 'grade': 'B'}
]
  
# Sort students data by grade key.
students = sorted(students, 
                  key = itemgetter('grade'))
  
# Display data grouped by grade
for key, value in groupby(students,
                          key = itemgetter('grade')):
    print(key)
    for k in value:
        print(k)

chevron_right


Output:

A
{'mark': '86', 'grade': 'A'}
{'mark': '91', 'grade': 'A'}
B
{'mark': '73', 'grade': 'B'}
{'mark': '79', 'grade': 'B'}
C
{'mark': '65', 'grade': 'C'}
D
{'mark': '49', 'grade': 'D'}



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.