Given a dictionary in Python, write a Python program to find the sum of all Items in the dictionary.
Examples:
Input : {'a': 100, 'b':200, 'c':300}
Output : 600
Input : {'x': 25, 'y':18, 'z':45}
Output : 88
- Approach #1 : Using Inbuilt
sum()FunctionUse sum function to find the sum of dictionary values.
# Python3 Program to find sum of# all items in a Dictionary# Function to print sumdefreturnSum(myDict):sum=0foriinmyDict:sum=sum+myDict[i]returnsum# Driver Functiondict={'a':100,'b':200,'c':300}print("Sum :", returnSum(dict))chevron_rightfilter_noneOutput:
Sum : 600
- Approach #2 : Using For loop to iterate through values using
values()functionIterate through each value of the dictionary using
values()function and keep adding it to the sum.
# Python3 Program to find sum of# all items in a Dictionary# Function to print sumdefreturnSum(dict):sum=0foriindict.values():sum=sum+ireturnsum# Driver Functiondict={'a':100,'b':200,'c':300}print("Sum :", returnSum(dict))chevron_rightfilter_noneOutput:
Sum : 600
- Approach #3 : Using For loop to iterate through items of Dictionary
Iterate through each item of the dictionary and simply keep adding the values to the sum variable.
# Python3 Program to find sum of# all items in a Dictionary# Function to print sumdefreturnSum(dict):sum=0foriinmyDict:sum=sum+dict[i]returnsum# Driver Functiondict={'a':100,'b':200,'c':300}print("Sum :", returnSum(dict))chevron_rightfilter_noneOutput:
Sum : 600
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python program to multiply all the items in a dictionary
- Python - All possible items combination dictionary
- Python program to extract dictionary items for custom values
- Python program to convert URL Parameters to Dictionary items
- Dictionary Methods in Python | Set 1 (cmp(), len(), items()...)
- Python | Delete items from dictionary while iterating
- Python | Count number of items in a dictionary value that is a list
- Python | Get items in sorted order from given dictionary
- Python | Get first K items in dictionary
- Python | Type conversion of dictionary items
- Python | Sort the items alphabetically from given dictionary
- Python - Frequency of unequal items in Dictionary
- Python - Common items Dictionary Value List
- Python - Sorted order Dictionary items pairing
- Python - Assign list items to Dictionary
- Python - Extract dictionary items with List elements
- Python - Convert dictionary items to values
- Python - Remove K value items from dictionary nesting
- Python - Column Mapped Tuples to dictionary items
- Python - Append items at beginning of dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

