How to sum all the values in a dictionary?

nedblorf picture nedblorf · Feb 3, 2011 · Viewed 335.2k times · Source

Let's say I have a dictionary in which the keys map to integers like:

d = {'key1': 1,'key2': 14,'key3': 47}

Is there a syntactically minimalistic way to return the sum of the values in d—i.e. 62 in this case?

Answer

phihag picture phihag · Feb 3, 2011

As you'd expect:

sum(d.values())