ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

Here, we have a dictionary consisting of integer items and we need to find the sum of all items of the dictionary in Python.
Submitted by Shivang Yadav, on May 05, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Dictionary is a collection in python which is used to store data as Key:value pair.

Example:

dict = { "python" : 1, "C++" : 3, "javaScript" : 2 } 

Sum of all items of the dictionary

Here, we have a dictionary that has integer values as a value in the key:value pair. We need to find the sum of all items (value) of the dictionary.

Input: 
dict = {"python" : 1, "C++" : 3, "javaScript" : 2 }

Output:
sum of all items of the dictionary : 6

We simply need to add all the values of the dictionary and print the sum. This can be done in python in more than one way, we can either iterate through the collection or we can use built-in methods to find the sum.

Let's see how some of these ways work.

Method 1: Using the sum() method

Python provides a built-in sum() method to find the sum of all items of an iterable. And we can also pass the values of a dictionary in the sum method to find the sum. For this we will use another built-in python method values().

Syntax:

sum(iterable)

Program to find the sum of all items of the dictionary using sum() method

# Program to find the sum of all items of 
# the dictionary using sum() method

langRating = {"python" : 24, "C++" : 22, "javaScript" : 25, "Java" : 20, "R" : 21 }

sumVal = sum(langRating.values())

print("The dictionary is : ", langRating)
print("Sum of all items of the dictionary  :", sumVal)

Output:

The dictionary is :  {'R': 21, 'C++': 22, 'Java': 20, 'python': 24, 'javaScript': 25}
Sum of all items of the dictionary  : 112

Method 2: By iterating over dictionary and adding values

We can alternatively iterate over the dictionary and add each value to a sum variable and after completion print that variable.

Algorithm:

  • Initialize: sumVal = 0
  • Loop through the dictionary with i
    • sumVal += dict[i]
  • Print sumVal

For iteration also, we have multiple ways, we can either iteratie over the whole dictionary and access the values using their keys. Or we can directly iterate over the values and add them.

Program to find the sum of all items of the dictionary using for loop of dictionary

# Program to find the sum of all items of the
# dictionary using for loop of dictionary

langRating = {"python" : 24, "C++" : 22, "javaScript" : 25, "Java" : 20, "R" : 21 }

# calculation sum of all items using loop 
sumVal = 0
for i in langRating:
    sumVal += langRating[i]

print("The dictionary is : ", langRating)
print("Sum of all items of the dictionary  :", sumVal)

Output:

The dictionary is :  {'javaScript': 25, 'C++': 22, 'R': 21, 'Java': 20, 'python': 24}
Sum of all items of the dictionary  : 112

Program to find the sum of all items of the dictionary using for loop of values of dictionary

# Program to find the sum of all items of the 
# dictionary using for loop of dictionary

langRating = {"python" : 24, "C++" : 22, "javaScript" : 25, "Java" : 20, "R" : 21 }

# calculation sum of all items using loop 
sumVal = 0
for i in langRating.values():
    sumVal += i

print("The dictionary is : ", langRating)
print("Sum of all items of the dictionary  :", sumVal)

Output:

The dictionary is :  {'C++': 22, 'javaScript': 25, 'R': 21, 'Java': 20, 'python': 24}
Sum of all items of the dictionary  : 112

Python Dictionary Programs »

ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.