Write a Python program to sum all the items in a dictionary | Example code

  • by

Use for loop or sum() function to make Python program to sum all the items in a dictionary. In both method you have to use the values() function to get the value of the dictionary.

Python program to sum all the items in a dictionary

Simple example code to find the sum of dictionary values.

Using sum() function

my_dict = {'A': 100, 'B': 200, 'C': 300}

res = sum(my_dict.values())
print(res)

Output:

Write a Python program to sum all the items in a dictionary

Using for loop

Using For loop to iterate through values using values() function and keep adding it to the sum.

my_dict = {'A': 100, 'B': 200, 'C': 300}

sum = 0
for i in my_dict.values():
    sum = sum + i

print(sum)

Output: 600

Do comment if you have any doubts or suggestions on this Python sum dictionary topic.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.