Python program to find the sum of all values of a dictionary

Python program to find the sum of all the values of a dictionary :

In this tutorial, we will learn how to find the sum of all the values of a dictionary in python. Algorithm of the program is as below :

Algorithm :

1. Create one dictionary with some key-value pairs. Alternatively, we can also take the inputs from user. But in this example, we are creating one dictionary at start.
2. ’.values()’ is used to get the values for all keys in the dictionary.
3. ’sum()’ method is used to get the sum of all the values.
4. Store the sum in a variable.
5. Print out the sum.

Python Program :

my_dictionary = {'1' : 1,'2' : 2,'3' : 3,'4' : 4,'5' : 5}

print("Given Dictionary : ",my_dictionary)
print("values ",my_dictionary.values())

sum_values = sum(my_dictionary.values())

print("Sum of all the values in the dictionary is : ",sum_values)

Output :

Given Dictionary :  {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5}
values  dict_values([1, 2, 3, 4, 5])
Sum of all the values in the dictionary is :  15

You can modify the program to get the inputs of the dictionary from user.

Leave a Reply