I have a list of dictionaries like that :
a = [
{'user_id':'111','clean_label':'VIR SEPA'},
{'user_id':'112','clean_label':'VIR SEPA'},
{'user_id':'111','clean_label':'VIR SEPA'},
]
and I want that :
a = [
[
{'user_id':'111','clean_label':'VIR SEPA'},
{'user_id':'111','clean_label':'VIR SEPA'}
],
[
{'user_id':'112','clean_label':'VIR SEPA'}
]
]
I tried with sorted and groupby from itertools like that :
sorted(a,key=lambda x: (x['user_id'],x['clean_label']))
[ [tr for tr in tr_per_user_id_clean_label] for key, tr_per_user_id_clean_label in itertools.groupby(a, key=lambda x: (x['user_id'], x['clean_label'])) ]
but I get that :
[[{'user_id': '111', 'clean_label': 'VIR SEPA'}],
[{'user_id': '112', 'clean_label': 'VIR SEPA'}],
[{'user_id': '111', 'clean_label': 'VIR SEPA'}]]
Can someone help me ??
*Edit : when I sort a :
[{'user_id': '111', 'clean_label': 'VIR SEPA'},
{'user_id': '111', 'clean_label': 'VIR SEPA'},
{'user_id': '112', 'clean_label': 'VIR SEPA'}]
itertools.groupby
is not really the ideal tool for this.You can achieve your goal with O(n) complexity using a
defaultdict
(vs O(n log n) withgroupby
as you need to sort):alternative with
setdefault
:output:
If the output needs to be sorted by user_id: