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 »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.