Python program to find the sum of all items in a dictionary
Last Updated :
11 Jul, 2025
The task of finding the sum of all items in a dictionary in Python involves calculating the total of all values stored in a dictionary. For example, given a dictionary {'a': 100, 'b': 200, 'c': 300}, the sum of values would be 100 + 200 + 300 = 600.
Using sum()
This is the simplest and fastest method to find the sum of all dictionary values. It directly accesses all values using d.values() and passes them to the sum() function. This approach is highly efficient as it avoids extra list creation and leverages Python’s built-in optimization.
Python
d = {'a': 100, 'b': 200, 'c': 300}
res = sum(d.values())
print(res)
Explanation: res = sum(d.values()) calculates the sum of all values in the dictionary by using the values() method to retrieve the values and passing them to the sum() function.
Using list comprehension
This method creates a list containing the dictionary values using list comprehension and then applies sum(). It is a clean and readable approach, but slightly slower than sum(d.values()) because it constructs a list in memory. It can be useful when additional processing is needed while extracting values.
Python
d = {'a': 100, 'b': 200, 'c': 300}
res = sum([d[key] for key in d])
print(res)
Explanation : sum([d[key] for key in d]) creates a list of values from the dictionary d using list comprehension and then calculates the sum of those values using the sum() function.
Using loop
This is a traditional approach using a for loop and an accumulator variable to incrementally sum the values. It is clear and easy to understand, especially for beginners. While efficient, it is slightly slower than sum(d.values()) due to manual addition in each iteration.
Python
d = {'a': 100, 'b': 200, 'c': 300}
res = 0
for value in d.values():
res += value
print(res)
Explanation: for loop iterate through the values of the dictionary d . In each iteration, the current value is added to the res variable using res += value. After the loop completes, the print(res) statement outputs the final sum of all dictionary values.
Using map()
map() extract values from the dictionary using a lambda function. It is considered functional programming style, but less readable for simple sum operations. While it avoids list creation, the lambda evaluation adds slight overhead, making it less efficient than sum(d.values()).
Python
d = {'a': 100, 'b': 200, 'c': 300}
res = sum(map(lambda key: d[key], d))
print(res)
Explanation: lambda key: d[key] retrieves the value corresponding to each key and map() applies this to all keys. sum() function then calculates the sum of all these values.
Similar Reads
Python Program to print sum of all key value pairs in a Dictionary Given a dictionary arr consisting of N items, where key and value are both of integer type, the task is to find the sum of all key value pairs in the dictionary. Examples: Input: arr = {1: 10, 2: 20, 3: 30}Output: 11 22 33Explanation: Sum of key and value of the first item in the dictionary = 1 + 10
5 min read
Python program to find Cumulative sum of a list Calculating the cumulative sum of a list means finding the running total of the elements as we move through the list. In this article, we will explore How to find the cumulative sum of a list. Using itertools.accumulate()This is the most efficient method for calculating cumulative sums. itertools mo
3 min read
Python program to Sort a List of Dictionaries by the Sum of their Values Given Dictionary List, sort by summation of their values. Input : test_list = [{1 : 3, 4 : 5, 3 : 5}, {1 : 100}, {8 : 9, 7 : 3}] Output : [{8: 9, 7: 3}, {1: 3, 4: 5, 3: 5}, {1: 100}] Explanation : 12 < 13 < 100, sorted by values sum Input : test_list = [{1 : 100}, {8 : 9, 7 : 3}] Output : [{8:
6 min read
Output of Python Programs | Set 24 (Dictionary) Prerequisite : Python-Dictionary1. What will be the output?Python dictionary = {"geek":10, "for":45, "geeks": 90} print("geek" in dictionary) Options: 10FalseTrueErrorOutput:3. TrueExplanation: in is used to check the key exist in dictionary or not. 2. What will be the output?Python dictionary ={1:"
2 min read
Python - List of dictionaries all values Summation Given a list of dictionaries, extract all the values summation. Input : test_list = [{"Apple" : 2, "Mango" : 2, "Grapes" : 2}, {"Apple" : 2, "Mango" : 2, "Grapes" : 2}] Output : 12 Explanation : 2 + 2 +...(6-times) = 12, sum of all values. Input : test_list = [{"Apple" : 3, "Mango" : 2, "Grapes" : 2
5 min read
Python program to find the sum of the value in the dictionary where the key represents the frequency Given a dictionary with a values list, where the key represents frequency, compute the total occurrence of each value in values lists. Input : test_dict = {70 : [7, 4, 6], 50 : [6, 8, 5, 2]} Output : {7: 70, 4: 70, 6: 120, 8: 50, 5: 50, 2: 50} Explanation : 6 occurs in both keys, hence 70 + 50 = 120
3 min read