Nazmul Hasan
Nazmul Hasan

Reputation: 7040

How to sum dict elements

In Python, I have list of dicts:

dict1 = [{'a':2, 'b':3},{'a':3, 'b':4}]

I want one final dict that will contain the sum of all dicts. I.e the result will be: {'a':5, 'b':7}

N.B: every dict in the list will contain same number of key, value pairs.

Upvotes: 43

Views: 72441

Answers (11)

SiggyF
SiggyF

Reputation: 23165

You can also use the pandas sum function to compute the sum:

import pandas as pd
# create a DataFrame
df = pd.DataFrame(dict1)
# compute the sum and convert to dict.
dict(df.sum())

This results in:

{'a': 5, 'b': 7}

It also works for floating points:

dict2 = [{'a':2, 'b':3.3},{'a':3, 'b':4.5}]
dict(pd.DataFrame(dict2).sum())

Gives the correct results:

{'a': 5.0, 'b': 7.8}

Upvotes: 5

Ste
Ste

Reputation: 83

Here is another working solution (python3), quite general as it works for dict, lists, arrays. For non-common elements, the original value will be included in the output dict.

def mergsum(a, b):
    for k in b:
        if k in a:
            b[k] = b[k] + a[k]
    c = {**a, **b}
    return c

dict1 = [{'a':2, 'b':3},{'a':3, 'b':4}]
print(mergsum(dict1[0], dict1[1]))

Upvotes: 1

trudolf
trudolf

Reputation: 2111

I was interested in the performance of the proposed Counter, reduce and sum methods for large lists. Maybe someone else is interested in this as well. You can have a look here: https://gist.github.com/torstenrudolf/277e98df296f23ff921c

I tested the three methods for this list of dictionaries:

dictList = [{'a': x, 'b': 2*x, 'c': x**2} for x in xrange(10000)]

the sum method showed the best performance, followed by reduce and Counter was the slowest. The time showed below is in seconds.

In [34]: test(dictList)
Out[34]: 
{'counter': 0.01955194902420044,
 'reduce': 0.006518083095550537,
 'sum': 0.0018319153785705566}

But this is dependent on the number of elements in the dictionaries. the sum method will slow down faster than the reduce.

l = [{y: x*y for y in xrange(100)} for x in xrange(10000)]

In [37]: test(l, num=100)
Out[37]: 
{'counter': 0.2401433277130127,
 'reduce': 0.11110662937164306,
 'sum': 0.2256883692741394}

Upvotes: 5

Aaron McMillin
Aaron McMillin

Reputation: 2667

One further one line solution

dict(
    functools.reduce(
        lambda x, y: x.update(y) or x,  # update, returns None, and we need to chain.
        dict1,
        collections.Counter())
)

This creates only one counter, uses it as an accumulator and finally converts back to a dict.

Upvotes: 0

Kyan
Kyan

Reputation: 403

Here is a reasonable beatiful one.

final = {}
for k in dict1[0].Keys():
    final[k] = sum(x[k] for x in dict1)
return final

Upvotes: 2

John La Rooy
John La Rooy

Reputation: 304215

Leveraging sum() should get better performance when adding more than a few dicts

>>> dict1 = [{'a':2, 'b':3},{'a':3, 'b':4}]
>>> from operator import itemgetter
>>> {k:sum(map(itemgetter(k), dict1)) for k in dict1[0]}        # Python2.7+
{'a': 5, 'b': 7}
>>> dict((k,sum(map(itemgetter(k), dict1))) for k in dict1[0])  # Python2.6
{'a': 5, 'b': 7}

adding Stephan's suggestion

>>> {k: sum(d[k] for d in dict1) for k in dict1[0]}            # Python2.7+
{'a': 5, 'b': 7}
>>> dict((k, sum(d[k] for d in dict1)) for k in dict1[0])      # Python2.6
{'a': 5, 'b': 7}

I think Stephan's version of the Python2.7 code reads really nicely

Upvotes: 13

paxdiablo
paxdiablo

Reputation: 881573

The following code shows one way to do it:

dict1 = [{'a':2, 'b':3},{'a':3, 'b':4}]

final = {}
for k in dict1[0].keys():           # Init all elements to zero.
    final[k] = 0
for d in dict1:
    for k in d.keys():
        final[k] = final[k] + d[k]  # Update the element.

print final

This outputs:

{'a': 5, 'b': 7}

as you desired.

Or, as inspired by kriss, better but still readable:

dict1 = [{'a':2, 'b':3},{'a':3, 'b':4}]

final = {}
for d in dict1:
    for k in d.keys():
        final[k] = final.get(k,0) + d[k]

print final

I pine for the days of the original, readable Python :-)

Upvotes: 6

Dave Kirby
Dave Kirby

Reputation: 26552

In Python 2.7 you can replace the dict with a collections.Counter object. This supports addition and subtraction of Counters.

Upvotes: 2

SiggyF
SiggyF

Reputation: 23165

You can use the collections.Counter

counter = collections.Counter()
for d in dict1: 
    counter.update(d)

Or, if you prefer oneliners:

functools.reduce(operator.add, map(collections.Counter, dict1))

Upvotes: 61

Manoj Govindan
Manoj Govindan

Reputation: 74705

This might help:

def sum_dict(d1, d2):
    for key, value in d1.items():
        d1[key] = value + d2.get(key, 0)
    return d1

>>> dict1 = [{'a':2, 'b':3},{'a':3, 'b':4}]
>>> reduce(sum_dict, dict1)
{'a': 5, 'b': 7}

Upvotes: 10

carl
carl

Reputation: 50554

A little ugly, but a one-liner:

dictf = reduce(lambda x, y: dict((k, v + y[k]) for k, v in x.iteritems()), dict1)

Upvotes: 18

Related Questions