• 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
  • Ruby
  • 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 key in ["c","d","e"]))

#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 set_value – Using at() Function to Set a Value in DataFrame
  • 2.  Draw Rectangle in Python Using turtle Module
  • 3.  How to Rotate a List in Python
  • 4.  Check if All Elements in Array are Equal in Python
  • 5.  How to Add Two Numbers in Python
  • 6.  Using Python to Compare Strings Alphabetically
  • 7.  Python if else do nothing – Using pass Statement to Do Nothing in Python
  • 8.  Get Current Year in Python
  • 9.  Shift Values in a List Using Python
  • 10.  How to Group By Columns and Count Rows in pandas DataFrame

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy