Q:

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

belongs to collection: Python Dictionary Programs

0

Python programming language is a high-level and object-oriented programming languagePython 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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Dictionary Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to remove a key from dictionary in Python?... >>
<< Python program to swap the position of dictionary ...