Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Group a list of dicts by key value, count separate key value as dict? [duplicate]

I'm trying to group up the name key values here as a key for a dict value, and count the source value as a key for said parent key, and have the count value with as its value.

data = [
{'name':'Gill', 'source':'foo'},
{'name':'Gill', 'source':'foo'},
{'name':'Gill', 'source':'foo'},
{'name':'Gill', 'source':'bar'},
{'name':'Gill', 'source':'bar'},
{'name':'Gill', 'source':'bar'},
{'name':'Gill', 'source':'bar'},
{'name':'Gill', 'source':'bar'},
{'name':'Dave', 'source':'foo'},
{'name':'Dave', 'source':'foo'},
{'name':'Dave', 'source':'foo'},
{'name':'Dave', 'source':'foo'},
{'name':'Dave', 'source':'egg'},
{'name':'Dave', 'source':'egg'},
{'name':'Dave', 'source':'egg'},
{'name':'Dave', 'source':'egg'},
{'name':'Dave', 'source':'egg'},
{'name':'Dave', 'source':'egg'},
{'name':'Dave', 'source':'egg'}
]

How do I achieve the below output?

{'Gill': {'foo':3, 'bar':5}, 'Dave': {'foo':4, 'egg':7}}

I think it may be possible with a 1 liner...

like image 757
Slopax Avatar asked Dec 21 '25 14:12

Slopax


1 Answers

Use itertools.groupby to group by names, then collections.Counter to count the source categories belonging to each name:

from collections import Counter
from itertools import groupby

f = lambda x: x['name']
dct = {k: Counter(d['source'] for d in g) for k, g in groupby(data, f)}
print(dct)
# {'Gill': Counter({'bar': 5, 'foo': 3}), 'Dave': Counter({'egg': 7, 'foo': 4})}
like image 114
Moses Koledoye Avatar answered Dec 24 '25 03:12

Moses Koledoye



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!