How to sum all the values in a dictionary?

前端 未结 9 1894
一生所求
一生所求 2020-11-28 01:39

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

9条回答
  •  离开以前
    2020-11-28 02:06

    You can get a generator of all the values in the dictionary, then cast it to a list and use the sum() function to get the sum of all the values.

    Example:

    c={"a":123,"b":4,"d":4,"c":-1001,"x":2002,"y":1001}
    
    sum(list(c.values()))
    

提交回复
热议问题