Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sum elements in list of dictionaries if two key values are the same

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?

like image 302
user3200392 Avatar asked Aug 27 '18 05:08

user3200392


People also ask

Can two dictionary keys have the same value?

No, each key in a dictionary should be unique. You can't have two keys with the same value. Attempting to use the same key again will just overwrite the previous value stored. If a key needs to store multiple values, then the value associated with the key should be a list or another dictionary.

How do you sum a list of values in a dictionary?

To sum the values in a list of dictionaries: Use a generator expression to iterate over the list. On each iteration, access the current dictionary at the specific key. Pass the generator expression to the sum() function.

Can you have two of the same keys in a dictionary Python?

The straight answer is NO. You can not have duplicate keys in a dictionary in Python.


1 Answers

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'}]
like image 185
cs95 Avatar answered Oct 08 '22 15:10

cs95