How To Sum All Values In A Dictionary In Python?

Sum all values in a dictionary in Python

Learning to sum all values in a dictionary in Python is one of the most basic knowledge when exposed to programming. To help you absorb knowledge easily, please continue reading the tutorial below.

Sum all values in a dictionary in Python

“Sum of all values in a dictionary in Python” is a pretty easy task. Here we will do it in the following two ways:

Using For Loop

Syntax:

for value in sequence:
    #loop body

Parameters:

  • value: represents the element of the sequence on each iteration.
  • sequence: an iterable datatype such as a list, a tuple, a set, or a string.

Firstly, we create a cumulative variable with the value 0. Secondly, we use a for loop to iterate through the dictionary’s values. To do that, we use the values() function to convert the dictionary to a list of all the values. Finally, In each loop, we add the dictionary value to the accumulator. Like this:

salaries = {
  "Rose" : 2100,
  "Harry": 1500,
  "Jimmy": 3000,
  "Ben"  : 500,
  "Luke" : 1400
}

total_salary = 0

for salary in salaries.values():
  total_salary += salary

print(f"The total amount of money the company must pay employees is ${total_salary}.")

Output:

The total amount of money the company must pay employees is $8500.

Using sum() function

Syntax:

sum(iterable, start)

Parameters:

  • iterable: Any Python object that permits it to be iterated, such as strings, lists, tuples, etc.
  • start: A value is added to the return value. The default value is 0.

We first get the values in the list using values(). You can then call the sum method to get the sum of these values.

salaries = {
  "Rose" : 2100,
  "Harry": 1500,
  "Jimmy": 3000,
  "Ben"  : 500,
  "Luke" : 1400
}

total_salary = sum(salaries.values())
print(f"The total amount of money the company must pay employees is ${total_salary}.")

Output:

The total amount of money the company must pay employees is $8500.

Using reduce() function in functools

Syntax:

reduce(function, iterable)

Parameters:

  • function: A function with two parameters that takes two elements from iterable.
  • iterable: Any Python object that permits it to be iterated, such as strings, lists, tuples, etc.

The reduce() function is convenient for doing some computation on a dictionary. For example, it is easy to apply rolling computation to sequential pairs of values in a dictionary.

Before you want to use the reduce() function, you must import it with a line of code:

from functools import reduce

We use reduce() function to sum all values in a dictionary in Python. Like this:

from functools import reduce

salaries = {
  "Rose" : 2100,
  "Harry": 1500,
  "Jimmy": 3000,
  "Ben"  : 500,
  "Luke" : 1400
}

total_salary = reduce(lambda total, salary : total + salary, salaries.values())
print(f"The total amount of money the company must pay employees is ${total_salary}.")

Output:

The total amount of money the company must pay employees is $8500.

Summary

Above, we have shown you how to sum all values in a dictionary in Python. Hope you have more knowledge to solve the problem you are facing. To better understand the content of this article, practice rewriting today’s examples without looking at the code sample. Have a beautiful day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *