How to sum values of a Python dictionary?


It is pretty easy to get the sum of values of a Python dictionary. You can first get the values in a list using the dict.values(). Then you can call the sum method to get the sum of these values. 

example

d = {
   'foo': 10,
   'bar': 20,
   'baz': 30
}
print(sum(d.values()))

Output

This will give the output −

60

Lakshmi Srinivas
Lakshmi Srinivas

Programmer / Analyst / Technician

Updated on: 27-Aug-2023

24K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements