• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Calculate Sum of Dictionary Values in Python

Calculate Sum of Dictionary Values in Python

June 24, 2022 Leave a Comment

To sum the values of a dictionary in Python, the easiest way is with the Python sum() function used on the dictionary values.

d = {'a': 4, 'b': 5, 'c': 6}

print(sum(d.values())

#Output:
15

You can also use comprehension to sum the values of a dictionary in Python.

d = {'a': 4, 'b': 5, 'c': 6}

print(sum(d[x] for x in d)

#Output:
15

Finally, of course, you can also use a loop to sum the dictionary values of a Python dictionary.

d = {'a': 4, 'b': 5, 'c': 6}

sum = 0

for x in d:
    sum = sum + d[x]

print(sum)

#Output:
15

When working with collections of data in Python, the ability to easily be able to calculate statistics is valuable.

One such situation is if you want to sum up the values in a dictionary.

You can easily sum the values of a dictionary in Python with the sum() function used on the dictionary values.

To get the values of a dictionary, you can use values() and then take the sum with sum().

Below shows you a simple example of how to easily find the sum of values in a dictionary in Python.

d = {'a': 4, 'b': 5, 'c': 6}

print(sum(d.values())

#Output:
15

How to Sum Dictionary Values Based on Key Name in Python

If you only want to sum certain values in a dictionary based on the name of the keys, then you have to do a little more work.

The easiest way to sum the values of a dictionary based on a condition is to use comprehension.

Below is a simple example showing you how to use comprehension to sum certain values of a dictionary with Python.

d = {'a': 4, 'b': 5, 'c': 6, 'd':7, 'e':8}

print(sum(value for key, value in d.items() if value > 5))

#Output:
21

Hopefully this article has been useful for you to learn how to get the sum of values in a dictionary in Python.

Other Articles You'll Also Like:

  • 1.  pandas percentile – Calculate Percentiles of Series or Columns in DataFrame
  • 2.  PROC FREQ Equivalent in Python
  • 3.  How to Group and Aggregate By Multiple Columns in Pandas
  • 4.  How to Check if Number is Divisible by 3 in Python
  • 5.  pandas head – Return First n Rows from DataFrame
  • 6.  List of Turtle Shapes in Python
  • 7.  islower Python – Check if All Letters in String Are Lowercase
  • 8.  Sort Series in pandas with sort_values() Function
  • 9.  Using Python to Read File Word by Word
  • 10.  Python Prepend to List with insert() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy