3 ms·
Here is a 1-liner using pandas. I would be interested if it's possible to do it in one-line using something from itertools. import pandas as pd [{'thi
by oli5679 5y ago
Here is a 1-liner using pandas.
I would be interested if it's possible to do it in one-line using something from itertools.
import pandas as pd
[{'thing':x, 'count':y} for x,y in pd.DataFrame(mylist).groupby('thing' ['count'].sum().to_dict().items()]
- psyklic 5y agoHere is a one-liner standard library version: from itertools import groupby keyfunc = lambda x: x['thing'] [{'thing': name, 'count': sum(t['count'] for t in things)} for name,things in groupby(sorted(mylist, key=keyfunc), keyfunc)]
- oli5679 5y agoThanks this is great
- psyklic 5y agoHere is a more concise Pandas version (though Pandas may be overkill for this task): import pandas as pd pd.DataFrame(mylist).groupby('thing', as_index=False).sum().to_dict('records')
- oli5679 5y agoThanks this is great