Reputation: 605
I have the following list of dictionaries:
dictionary =[{'Flow': 100, 'Location': 'USA', 'Name': 'A1'},
{'Flow': 90, 'Location': 'Europe', 'Name': 'B1'},
{'Flow': 20, 'Location': 'USA', 'Name': 'A1'},
{'Flow': 70, 'Location': 'Europe', 'Name': 'B1'}]
I want to create a new list of dictionaries, with summed Flow
values of all dictionaries where Location
and Name
are the same. My desired output would be:
new_dictionary =[{'Flow': 120, 'Location': 'USA', 'Name': 'A1'},
{'Flow': 160, 'Location': 'Europe', 'Name': 'B1'},]
How can I achieve this?
Upvotes: 14
Views: 6137
Reputation: 402493
This is possible, but non-trivial to implement in python. Might I suggest using pandas? This is simple with a groupby
, sum
, and to_dict
.
import pandas as pd
(pd.DataFrame(dictionary)
.groupby(['Location', 'Name'], as_index=False)
.Flow.sum()
.to_dict('r'))
[{'Flow': 160, 'Location': 'Europe', 'Name': 'B1'},
{'Flow': 120, 'Location': 'USA', 'Name': 'A1'}]
To install, use pip install --user pandas
.
Otherwise, you can apply a pseudo-generic group operation using itertools.groupby
.
from itertools import groupby
from operator import itemgetter
grouper = ['Location', 'Name']
key = itemgetter(*grouper)
dictionary.sort(key=key)
[{**dict(zip(grouper, k)), 'Flow': sum(map(itemgetter('Flow'), g))}
for k, g in groupby(dictionary, key=key)]
[{'Flow': 160, 'Location': 'Europe', 'Name': 'B1'},
{'Flow': 120, 'Location': 'USA', 'Name': 'A1'}]
Upvotes: 17
Reputation: 8930
Not exactly the output you expect, but..
Using collections.Counter()
count = Counter()
for i in dictionary:
count[i['Location'], i['Name']] += i['Flow']
print count
Will give:
Counter({ ('Europe', 'B1'): 160,
('USA', 'A1'): 120 })
I hope this will at least give you some idea.
Upvotes: 6
Reputation: 33671
While I would also prefer using Pandas if possible, here is solution using plain python:
In [1]: import itertools
In [2]: dictionary =[{'Flow': 100, 'Location': 'USA', 'Name': 'A1'},
...: {'Flow': 90, 'Location': 'Europe', 'Name': 'B1'},
...: {'Flow': 20, 'Location': 'USA', 'Name': 'A1'},
...: {'Flow': 70, 'Location': 'Europe', 'Name': 'B1'}]
...:
In [3]: import operator
In [4]: key = operator.itemgetter('Location', 'Name')
In [5]: [{'Flow': sum(x['Flow'] for x in g),
...: 'Location': k[0],
...: 'Name': k[1]}
...: for k, g in itertools.groupby(sorted(dictionary, key=key), key=key)]
...:
...:
Out[5]:
[{'Flow': 160, 'Location': 'Europe', 'Name': 'B1'},
{'Flow': 120, 'Location': 'USA', 'Name': 'A1'}]
Another way is to use defaultdict, which gives you a slightly different representation (though you can convert it back to list of dicts if you want):
In [11]: import collections
In [12]: cnt = collections.defaultdict(int)
In [13]: for r in dictionary:
...: cnt[(r['Location'], r['Name'])] += r['Flow']
...:
In [14]: cnt
Out[14]: defaultdict(int, {('Europe', 'B1'): 160, ('USA', 'A1'): 120})
In [15]: [{'Flow': x, 'Location': k[0], 'Name': k[1]} for k, x in cnt.items()]
Out[15]:
[{'Flow': 120, 'Location': 'USA', 'Name': 'A1'},
{'Flow': 160, 'Location': 'Europe', 'Name': 'B1'}]
Upvotes: 10