I am using itertools
to group by
a dictionary key
using the below:
JavaScript
x
7
1
host_data = []
2
for k,v in itertools.groupby(temp_data, key=lambda x:x['device_id'])
3
d = {}
4
for dct in v:
5
d.update(dct)
6
host_data.append(d)
7
However I would like to group by ‘device_id’ and ‘port_id’ if possible, how can I add an additional key to the grouping?
Advertisement
Answer
Just use a tuple as key:
JavaScript
1
2
1
itertools.groupby(temp_data, key=lambda x:(x['device_id'], x['port_id']))
2