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

In this program, we are using the sum function, and dictionary values function to find the sum of dictionary values. The sum function is to return the sum of all the values 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

Calculate Sum of Items in a Dictionary using values()

This program uses For Loop along with the values function to add values 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 Dictionary items output.

Python Program to find Sum of Items in a Dictionary using values() function

Sum of Items in a Dictionary using for loop

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

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)
Python Program to find Sum of Items in a Dictionary using for loop