Python Program to find Sum of Items in a Dictionary

Write a Python Program to find the Sum of Items in a Dictionary with a practical example.

Python Program to find Sum of Items in a Dictionary Example 1

In this program we are using sum function, and dictionary values function to find the sum of dictionary values. The Python sum function is to return the sum of all the values in a Dictionary

# Python Program to find Sum of Items in a Dictionary

myDict = {'x': 250, 'y':500, 'z':410}
print("Dictionary: ", myDict)

# Print Values using get
print("\nSum of Values: ", sum(myDict.values()))
Python Program to find Sum of all Items in a Dictionary 1

Python Program to Calculate Sum of Items in a Dictionary Example 2

This Python program uses For Loop along with values function to add values in a dictionary.

# Python Program to find Sum of Items in a Dictionary

myDict = {'x': 250, 'y':500, 'z':410}
print("Dictionary: ", myDict)
total = 0

# Print Values using get
for i in myDict.values():
    total = total + i
    
print("\nThe Total Sum of Values : ", total)

The sum of Python Dictionary items output

Dictionary:  {'x': 250, 'y': 500, 'z': 410}

The Total Sum of Values :  1160

Python Program to Calculate Sum of all Items in a Dictionary Example 3

In this Python program, we are using For Loop to iterate each element in this Dictionary. Inside the Python loop, we are adding those dictionary values to the total variable.

# Python Program to find Sum of Items in a Dictionary

myDict = {'x': 250, 'y':500, 'z':410}
print("Dictionary: ", myDict)
total = 0

# Print Values using get
for i in myDict:
    total = total + myDict[i]
    
print("\nThe Total Sum of Values : ", total)
Dictionary:  {'x': 250, 'y': 500, 'z': 410}

The Total Sum of Values :  1160