Python program to find the sum of all items in a dictionary
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 : 600Input : {‘x’: 25, ‘y’:18, ‘z’:45}
Output : 88
Using Inbuilt sum() Function
Use the sum function to find the sum of dictionary values.
Python3
# Python3 Program to find sum of # all items in a Dictionary # Function to print sum def returnSum(myDict): list = [] for i in myDict: list .append(myDict[i]) final = sum ( list ) return final # Driver Function dict = { 'a' : 100 , 'b' : 200 , 'c' : 300 } print ( "Sum :" , returnSum( dict )) |
Output:
Sum : 600
Using For loop to iterate through values using values() function
Iterate through each value of the dictionary using values() function and keep adding it to the sum.
Python3
# Python3 Program to find sum of # all items in a Dictionary # Function to print sum def returnSum( dict ): sum = 0 for i in dict .values(): sum = sum + i return sum # Driver Function dict = { 'a' : 100 , 'b' : 200 , 'c' : 300 } print ( "Sum :" , returnSum( dict )) |
Output:
Sum : 600
Using for loop to iterate over each value in a dictionary
Iterate through each item of the dictionary and simply keep adding the values to the sum variable.
Python3
# Python3 Program to find sum of # all items in a Dictionary # Function to print sum def returnSum( dict ): sum = 0 for i in dict : sum = sum + dict [i] return sum # Driver Function dict = { 'a' : 100 , 'b' : 200 , 'c' : 300 } print ( "Sum :" , returnSum( dict )) |
Output:
Sum : 600
Using values() and sum() function
Here we are using the dictionary.sum() method to find the sum of values taken out through dictionary.values() function
Python3
# Python3 Program to find sum of # all items in a Dictionary # Function to print sum def returnSum( dict ): return sum ( dict .values()) # Driver Function dict = { 'a' : 100 , 'b' : 200 , 'c' : 300 } print ( "Sum :" , returnSum( dict )) |
Output:
Sum : 600
Using the lambda function
The lambda function is used to calculate the summation of all the values in the dictionary
Python3
import functools dic = { 'a' : 100 , 'b' : 200 , 'c' : 300 } sum_dic = functools. reduce ( lambda ac,k: ac + dic[k], dic, 0 ) print ( "Sum :" , sum_dic) |
Output:
Sum : 600
Using a generator expression and the built-in sum function
Another approach to find the sum of all items in a dictionary is to use a generator expression and the built-in sum function. A generator expression is an expression that generates a sequence of values, and is similar to a list comprehension, but does not create a new list.
For example:
Python3
def returnSum(myDict): return sum (myDict[key] for key in myDict) dict = { 'a' : 100 , 'b' : 200 , 'c' : 300 } print ( "Sum :" , returnSum( dict )) #This code is contributed by Edula Vinay Kumar Reddy |
Sum : 600
This approach has a time complexity of O(n), where n is the number of items in the dictionary, since it involves iterating through the dictionary to generate the sequence of values. The space complexity is also O(n), since it generates a sequence of values that is the same size as the original dictionary.
Please Login to comment...