I have the following list in Python:
my_list = [{'code_id': 'A', 'amount': 100.0},{'code_id': 'B', 'amount': 150.0},{'code_id': 'C', 'amount': 200.0},{'code_id': 'A', 'amount': 120.0},{'code_id': 'B', 'amount': 300.0},{'code_id': 'D', 'amount': 180.0}]
From the list above, I need to create a new list with no duplicates; but sum the "amount" for all items in the above list.
I need to achieve a final list like:
final_list = [{'code_id': 'A', 'amount': 220.0},{'code_id': 'B', 'amount': 450.0},{'code_id': 'C', 'amount': 200.0},{'code_id': 'D', 'amount': 180.0}]
I was able to remove duplicates but do not know how to sum values in the process. The sample code I have used:
final_list = []
seen = set()
for dic in my_list:
key = (dic['code_id'])
if key in seen:
continue
final_list.append(dic)
seen.add(key)
How can I achieve this in Python?
CodePudding user response:
I would start with a temporary defaultdict
that keeps track of the 'amount'
for each 'code_id'
:
from collections import defaultdict
my_list = [{'code_id': 'A', 'amount': 100.0},{'code_id': 'B', 'amount': 150.0},{'code_id': 'C', 'amount': 200.0}, {'code_id': 'A', 'amount': 120.0},{'code_id': 'B', 'amount': 300.0},{'code_id': 'D', 'amount': 180.0}]
tmp = defaultdict(int)
for d in my_list:
tmp[d['code_id']] = d['amount']
# if tmp was a normal dict, you could use
# tmp[d['code_id']] = tmp.get(d['code_id'], 0) d['amount']
print(tmp)
# defaultdict(int, {'A': 220.0, 'B': 450.0, 'C': 200.0, 'D': 180.0})
... and then transform the structure of tmp
to arrive at the desired result
result = [{'code_id': k, 'amount': v} for k, v in tmp.items()]
print(result)
# [{'code_id': 'A', 'amount': 220.0}, {'code_id': 'B', 'amount': 450.0}, {'code_id': 'C', 'amount': 200.0}, {'code_id': 'D', 'amount': 180.0}]
For the pandas
users out there:
>>> pd.DataFrame(my_list).groupby('code_id', as_index=False).sum().to_dict(orient='records')
[{'code_id': 'A', 'amount': 220.0},
{'code_id': 'B', 'amount': 450.0},
{'code_id': 'C', 'amount': 200.0},
{'code_id': 'D', 'amount': 180.0}]