Search code examples
pythondictionaryhashsum

How to sum all the values in a dictionary?


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?


Solution

  • As you'd expect:

    sum(d.values())