This post will discuss how to calculate the sum of all the dictionary’s values in Python.

1. Using sum() function

A simple solution is to use the built-in function sum() to calculate the sum of all the dictionary’s values. The idea is to get a view of the dictionary’s values using the dict.values() function and pass it to sum().

Download  Run Code

 
You can also achieve this with a list comprehension.

Download  Run Code

 
For floating-point dictionary’s values, consider using math.fsum() to get sum with extended precision.

2. Using reduce operation

Alternatively, you can use the functools.reduce() function to calculate the sum of the dictionary’s values with a lambda expression. Following is a simple example demonstrating usage of this:

Download  Run Code

That’s all about calculating the sum of all dictionary values in Python.