Sum all values in a dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Jul 3, 2022

banner

Photo from Unsplash

Sum all values in a dictionary in Python #

Use the sum() function to sum all values in a dictionary, e.g. total = sum(my_dict.values()). The values() method on the dictionary will return a view of the dictionary's values, which can directly be passed to the sum() function to get the sum.

main.py
my_dict = { 'one': 1, 'two': 2, 'three': 3, } total = sum(my_dict.values()) print(total) # 👉️ 6 # 👇️ [1, 2, 3] print(list(my_dict.values()))

We used the sum() function to sum all values in a dictionary.

The dict.values method returns a new view of the dictionary's values.

The sum function takes an iterable, sums its items from left to right and returns the total.

main.py
print(sum([1, 2, 3])) # 👉️ 6

The sum function takes the following 2 arguments:

NameDescription
iterablethe iterable whose items to sum
startsums the start value and the items of the iterable. sum defaults to 0 (optional)

Notice that the value for the optional start argument defaults to 0. This means that using this approach with an empty dictionary would return 0.

main.py
my_dict = {} total = sum(my_dict.values()) print(total) # 👉️ 0 # 👇️ [] print(list(my_dict.values()))

An alternative approach is to use the reduce() function.

main.py
from functools import reduce my_dict = { 'one': 1, 'two': 2, 'three': 3, } total = reduce( lambda acc, current: acc + current, my_dict.values() ) print(total) # 👉️ 6

Using the reduce() function is definitely not needed in this scenario as it is much more verbose than passing the view of the dictionary's values directly to the sum() function.

The reduce function takes the following 3 parameters:

NameDescription
functionA function that takes 2 parameters - the accumulated value and a value from the iterable.
iterableEach element in the iterable will get passed as an argument to the function.
initializerAn optional initializer value that is placed before the items of the iterable in the calculation.
The lambda function in the example takes the accumulated value and the current value as parameters and returns the sum of the two.

If we provide a value for the initializer argument, it is placed before the items of the iterable in the calculation.

main.py
from functools import reduce my_dict = { 'one': 1, 'two': 2, 'three': 3, } total = reduce( lambda acc, current: acc + current, my_dict.values(), 0 ) print(total) # 👉️ 6

In the example, we passed 0 for the initializer argument, so the value of the accumulator will be 0 on the first iteration.

The value of the accumulator would get set to the first element in the iterable if we didn't pass a value for the initializer.

If the iterable is empty and the initializer is provided, the initializer is returned.

main.py
from functools import reduce my_dict = {} total = reduce( lambda acc, current: acc + current, my_dict.values(), 0 ) print(total) # 👉️ 0
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.