Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sum list of dictionary values

I have a list of dictionary in this form :

[
{'signal_8': 1, 'signal_1': 7, 'signal_10': 5, 'signal_5': 2, 'signal_2': 5, 'signal_6': 3, 'signal_4': 9, 'signal_3': 6, 'signal_9': 4, 'signal_7': 8}, 

{'signal_8': 1, 'signal_1': 7, 'signal_10': 5, 'signal_5': 2, 'signal_2': 5, 'signal_6': 3, 'signal_4': 9, 'signal_3': 6, 'signal_9': 4, 'signal_7': 8},

{'signal_8': 1, 'signal_1': 7, 'signal_10': 5, 'signal_5': 2, 'signal_2': 5, 'signal_6': 3, 'signal_4': 9, 'signal_3': 6, 'signal_9': 4, 'signal_7': 8},
]

and I want to sum the values in this by key for each element in the list like that :

    {
     'signal_8': 3,
     'signal_1': 21,
     'signal_10': 15,
     'signal_5': 6,
     'signal_2': 15,
     'signal_6': 9,
     'signal_4': 27,
     'signal_3': 18,
     'signal_9': 12,
     'signal_7': 24
    }

what I have tried is the following :

    result = {}
    sm = 0
    for elm in original_list:
        for k,v in elm.items():
            sm += v
            result[k] = sm
    print(result)

but it still doesn't work.

like image 203
saul Avatar asked Jun 10 '18 09:06

saul


People also ask

How do I 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.

How do you get the sum of all values in a dictionary in Python?

It is pretty easy to get the sum of values of a python dictionary. You can first get the values in a list using the dict. values(). Then you can call the sum method to get the sum of these values.

Can I add list as dictionary value Python?

Method 1: Using += sign on a key with an empty value In this method, we will use the += operator to append a list into the dictionary, for this we will take a dictionary and then add elements as a list into the dictionary.

How do I generate a list of values from a dictionary key?

To get the list of dictionary values from the list of keys, use the list comprehension statement [d[key] for key in keys] that iterates over each key in the list of keys and puts the associated value d[key] into the newly-created list.


2 Answers

Similar to daveruinseverything's answer, I'd solve this with a Counter, but make use of its update method.

Let signals be your list of dicts.

>>> from collections import Counter
>>> c = Counter()
>>> for d in signals:
...     c.update(d)
... 
>>> c
Counter({'signal_4': 27, 'signal_7': 24, 'signal_1': 21, 'signal_3': 18, 'signal_10': 15, 'signal_2': 15, 'signal_9': 12, 'signal_6': 9, 'signal_5': 6, 'signal_8': 3})

For Op's sake, can you briefly describe what's happening here?

A Counter works similar to a dict, but its update method adds values to the values of pre-existing keys instead of overriding them.

like image 171
timgeb Avatar answered Oct 21 '22 04:10

timgeb


The problem with your code is that you are summing sm and v no matter the key. Below you can find a reformatted version of your code that works. It simply adds the values from each element from the list to the result object:

from collections import defaultdict

result = defaultdict(int)

for elm in original_list:
    for k, v in elm.items():
        result[k] += v
print(result)

Or, with a one liner you can have:

result = {key: sum(e[key] for e in original_list) for key in original_list[0].keys()}
like image 35
iulian Avatar answered Oct 21 '22 04:10

iulian