How to sum all the values in a dictionary?

前端 未结 9 1930
一生所求
一生所求 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 01:55

    d = {'key1': 1,'key2': 14,'key3': 47}
    sum1 = sum(d[item] for item in d)
    print(sum1)
    

    you can do it using the for loop

提交回复
热议问题