Write a Python program to sum all the items in a dictionary
In this post, you will learn how to sum all the items in a dictionary using the Python programming language.
A Dictionary is a collection whose values are accessible by key. The dictionary is a mapping of keys and values. The order of elements in a dictionary is undefined. We can iterate over keys, values and key-value pairs. The dict() is a constructor which is used to create instances of the class dict. The order of elements in a dictionary is undefined. We can iterate over keys, values, or key-value pairs. Here, we have mentioned different ways to sum all the items in a dictionary. To find out the sum of all the values in a dictionary, we will read the values associated with each key in the dictionary and add them.
Sum all the items in a dictionary using sum()
Here is the Python program to find the sum of all the items in a dictionary. The sum() is an in-built function of Python that, we can use to find the sum of all the values in the dictionary. We have used the values() method to get the values that are in the dictionary. This method returns a list of all the values that are in the dictionary. Then, we used the sum() method to print the sum of all the items in a dictionary. We have only used the in-built methods to get the sum of all items.
# dictionary initialisation
items ={'X':420,'Y':220,'Z':198}
print("Sum of values in the dictionary:")
print(sum(items.values()))
Output of the above code:
Sum of values in the dictionary:
838
Sum all the items in a dictionary using iterative approach
In the given Python program, we have defined a function sum_dict() and used a for loop to iterate through each item in the dictionary and added those items using the + operator. We have used the values() function to iterate through each value of the dictionary.
# Sum of values of a dictionary
# in Python
def sum_dict(items):
sum=0
#iterate through values
for i in items.values():
sum=sum+i
return sum
# dictionary initialisation
items ={'X':231,'Y':621,'Z':291}
print("Dictionary elements: ", items)
print("Sum of values in the dictionary: ",sum_dict(items))
Output of the above code:
Dictionary elements: {'X': 231, 'Y': 621, 'Z': 291}
Sum of values in the dictionary: 1143
Sum all the items in a dictionary using for loop
In the given Python program, we have used the for loop to iterate through each item in the dictionary and add the values to the sum variable.
# Function to print sum
def returnSum(dict):
sum = 0
for i in dict:
sum = sum + dict[i]
return sum
# Driver Function
dict ={'X':210,'Y':111,'Z':212}
print("Sum of values in the dictionary:", returnSum(dict))
Output of the above code:
Sum of values in the dictionary: 533
Related Articles
Calculate a Percentage in Python
Python ordereddict
Add key value pair to dictionary Python
Sort dictionary by value Python
Convert list to dictionary Python
Python dict inside list
Pandas string to datetime
Convert Excel to CSV Python Pandas
Python add list to list
Python Pandas Dataframe to CSV
Python compare two lists
Remove element from list Python
Python iterate list with index
Python program to sum all the numbers in a list
Python print without newline
Python iterate list with index
Python add list to list
Python convert xml to dict
Python dict inside list
Python convert dict to xml
Python raise keyword