Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python program to multiply all the items in a dictionary
Next article icon

Python program to find the sum of all items in a dictionary

Last Updated : 22 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of finding the sum of all items in a dictionary in Python involves calculating the total of all values stored in a dictionary. For example, given a dictionary {‘a’: 100, ‘b’: 200, ‘c’: 300}, the sum of values would be 100 + 200 + 300 = 600.

Using sum()

This is the simplest and fastest method to find the sum of all dictionary values. It directly accesses all values using d.values() and passes them to the sum() function. This approach is highly efficient as it avoids extra list creation and leverages Python’s built-in optimization.

Python
d = {'a': 100, 'b': 200, 'c': 300}  res = sum(d.values()) print(res) 

Output
600 

Explanation: res = sum(d.values()) calculates the sum of all values in the dictionary by using the values() method to retrieve the values and passing them to the sum() function.

Table of Content

  • Using list comprehension
  • Using loop
  • Using map()

Using list comprehension

This method creates a list containing the dictionary values using list comprehension and then applies sum(). It is a clean and readable approach, but slightly slower than sum(d.values()) because it constructs a list in memory. It can be useful when additional processing is needed while extracting values.

Python
d = {'a': 100, 'b': 200, 'c': 300}  res = sum([d[key] for key in d]) print(res) 

Output
600 

Explanation : sum([d[key] for key in d]) creates a list of values from the dictionary d using list comprehension and then calculates the sum of those values using the sum() function.

Using loop

This is a traditional approach using a for loop and an accumulator variable to incrementally sum the values. It is clear and easy to understand, especially for beginners. While efficient, it is slightly slower than sum(d.values()) due to manual addition in each iteration.

Python
d = {'a': 100, 'b': 200, 'c': 300} res = 0  for value in d.values():     res += value print(res) 

Output
600 

Explanation: for loop iterate through the values of the dictionary d . In each iteration, the current value is added to the res variable using res += value. After the loop completes, the print(res) statement outputs the final sum of all dictionary values.

Using map()

map() extract values from the dictionary using a lambda function. It is considered functional programming style, but less readable for simple sum operations. While it avoids list creation, the lambda evaluation adds slight overhead, making it less efficient than sum(d.values()).

Python
d = {'a': 100, 'b': 200, 'c': 300}  res = sum(map(lambda key: d[key], d)) print(res) 

Output
600 

Explanation: lambda key: d[key] retrieves the value corresponding to each key and map() applies this to all keys. sum() function then calculates the sum of all these values.  



Next Article
Python program to multiply all the items in a dictionary

S

Smitha Dinesh Semwal
Improve
Article Tags :
  • Python
  • Python Programs
  • Python dictionary-programs
  • python-dict
Practice Tags :
  • python
  • python-dict

Similar Reads

  • Python program to multiply all the items in a dictionary
    Python program to illustrate multiplying all the items in a dictionary could be done by creating a dictionary that will store all the key-value pairs, multiplying the value of all the keys, and storing it in a variable. Example: Input: dict = {'value1':5, 'value2':4, 'value3':3, 'value4':2, 'value5'
    2 min read
  • Python Program to find XOR of all the key value pairs in a Dictionary
    Given a dictionary in python, write a program to find the XOR of all the key-value pairs in the dictionary and return in the form of an array. Note: All the keys and values in the dictionary are integers. Examples: Input : dic={1:3, 4:5, 6:7, 3 :8}Output : [2, 1, 1, 11]Explanation: XOR of all the ke
    3 min read
  • Python Program to find XOR of values of all even keys in a dictionary
    Given a dictionary in Python, our task is to find the XOR of values of all even keys in a dictionary in Python. Note: All the keys and values in the dictionary are integers. Examples: Input : dic= {1:3, 4:5, 6:7, 3 :8}Output : 2Explanation: Even keys in the dictionary are 4,6 and their values are 5,
    5 min read
  • Python Program to print sum of all key value pairs in a Dictionary
    Given a dictionary arr consisting of N items, where key and value are both of integer type, the task is to find the sum of all key value pairs in the dictionary. Examples: Input: arr = {1: 10, 2: 20, 3: 30}Output: 11 22 33Explanation: Sum of key and value of the first item in the dictionary = 1 + 10
    5 min read
  • Adding Items to a Dictionary in a Loop in Python
    The task of adding items to a dictionary in a loop in Python involves iterating over a collection of keys and values and adding them to an existing dictionary. This process is useful when we need to dynamically build or update a dictionary, especially when dealing with large datasets or generating k
    3 min read
  • Python program to find sum of elements in list
    Finding the sum of elements in a list means adding all the values together to get a single total. For example, given a list like [10, 20, 30, 40, 50], you might want to calculate the total sum, which is 150. Let's explore these different methods to do this efficiently. Using sum()sum() function is t
    3 min read
  • Python program to find Cumulative sum of a list
    Calculating the cumulative sum of a list means finding the running total of the elements as we move through the list. In this article, we will explore How to find the cumulative sum of a list. Using itertools.accumulate()This is the most efficient method for calculating cumulative sums. itertools mo
    3 min read
  • Python program to find the sum of all even and odd digits of an integer list
    The following article shows how given an integer list, we can produce the sum of all its odd and even digits. Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.Input : test_list = [345, 893]
    5 min read
  • Python program to count Even and Odd numbers in a Dictionary
    Given a python dictionary, the task is to count even and odd numbers present in the dictionary. Examples: Input : {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e' : 5}Output : Even = 2, odd = 3Input : {'x': 4, 'y':9, 'z':16}Output : Even = 2, odd = 1 Approach using values() Function: Traverse the dictionary and
    3 min read
  • Python Program to Find Sum of Array
    Given an array of integers, find the sum of its elements. Examples: Input : arr[] = {1, 2, 3}Output : 6Explanation: 1 + 2 + 3 = 6This Python program calculates the sum of an array by iterating through each element and adding it to a running total. The sum is then returned. An example usage is provid
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences