Jun-30-2019, 12:00 PM
Pandas is a very good tool to perform grouping, e.g.
import pandas as pd df = pd.DataFrame(data) df.groupby(['m', 'n']).groups
Output:{('bad', 2): Int64Index([2, 3], dtype='int64'),
('bad', 3): Int64Index([4], dtype='int64'),
('good', 1): Int64Index([0, 1], dtype='int64')}
If you don't want to use Pandas, you can do this using standard Python packages only, e.g.from itertools import groupby from operator import itemgetter for a, b in groupby(data, key=lambda x: (itemgetter('m')(x), itemgetter('n')(x))): print(a, list(b))
Output:('good', 1) [{'name': 'John', 'm': 'good', 'n': 1}, {'name': 'Alina', 'm': 'good', 'n': 1}]
('bad', 2) [{'name': 'Olivia', 'm': 'bad', 'n': 2}, {'name': 'Ruby', 'm': 'bad', 'n': 2}]
('bad', 3) [{'name': 'Ruby', 'm': 'bad', 'n': 3}]
This might be not exactly you want. Nevertheless, these approaches may be useful: you can easily access/process the data in each group.