Python program to find the sum of all items in a dictionary
Last Updated :
22 Feb, 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 sum of elements in list Finding the sum of elements in a list means adding all the values together to get a single total. For example, given a list like [10, 20, 30, 40, 50], you might want to calculate the total sum, which is 150. Let's explore these different methods to do this efficiently.Using sum()sum() function is th
3 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 find the sum of all even and odd digits of an integer list The following article shows how given an integer list, we can produce the sum of all its odd and even digits. Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.Input : test_list = [345, 893]
5 min read
Python program to find the sum of Characters ascii values in String List Given the string list, the task is to write a Python program to compute the summation value of each character's ASCII value. Examples: Input : test_list = ["geeksforgeeks", "teaches", "discipline"] Output : [133, 61, 100] Explanation : Positional character summed to get required values. Input : test
4 min read