When working with data in Python, grouping by a certain key is a very common need. For example, when analyzing email logs, you might want to know how many emails were sent for each subject, or list the top five email providers for each subject. In a database, this is very simple. But what if you do not want to send the data to a database?
Besides Pandas, the powerful data processing library that can help with this, Python itself also has GroupBy built in!
The itertools module has a method called groupby. You pass in a collection and specify a key, and it organizes items with the same key into something list-like (though it is not actually a list). Then you can either count them (the number of emails sent with the same subject), or list the data for each shared key separately (the top five email providers by subject).
Before you group, you need to sort the data by the key you want to group on. See the code below for the details (Python 3). Here, a lambda is used to get the field to use as the key.
from itertools import groupby dicts = [ {'name': 'A', 'age': 16}, {'name': 'B', 'age': 18}, {'name': 'C', 'age': 17}, {'name': 'D', 'age': 16}, {'name': 'E', 'age': 17} ] dicts.sort(key=lambda d : d['age']) # Group By Age Field for key, groups in groupby(dicts, lambda d : d['age']): # print(list(groups)) # print(len(list(groups))) print(key) for item in groups: print(item) print('-----')