;

Python Program to Find the Sum of Items in a Dictionary


Tutorialsrack 06/05/2020 Python

In this Python program, we will learn how to find the sum of the items in a given dictionary. In this program, we are using three ways to do this. The first way to find the sum of items in a dictionary is the python dictionary built-in function sum() and values() and the second way to find the sum of the item is using loops and python dictionary built-in values() function and the third way is without using any python built-in function.

Here is the source code of the program to find the sum of the items in a given dictionary.

Program 1: Python Program to Find the Sum of Items in a Dictionary Using sum() and values() Function

In this program, we used the python built-in sum and values() function to find the sum of the items in a given dictionary. The sum() function takes an iterable and returns the sum of items in it. The sum() function adds the items of an iterable and returns the sum. And the values() function returns a list of all the values available in a given dictionary.

Python Program to Find the Sum of Items in a Dictionary Using sum() and values() Function
# Python Program to Find the Sum of Items in a Dictionary
# Using Sum() and values() Function

# Declares a Dictionary
myDict = {'x': 200, 'y':300, 'z':510}

# Print the Dictionary
print("Dictionary: ", myDict)

# Output
print("\nThe Sum of Values: ", sum(myDict.values()))
Output

Dictionary:  {'x': 200, 'y': 300, 'z': 510}

 

The Sum of Values:  1010

Program 2: Python Program to Find the Sum of Items in a Dictionary Using For Loop and values() Function

In this program, we used the for loop to iterate the dictionary and values() to get a list of all the items available in a given list.

Python Program to Find the Sum of Items in a Dictionary Using For Loop and values() Function
# Python Program to Find the Sum of Items in a Dictionary
# Using For Loop and values() Function

# Declares a Dictionary
myDict = {'x': 200, 'y':300, 'z':510}

# Print the Dictionary
print("Dictionary: ", myDict)
total = 0

# Print Values
for i in myDict.values():
    total = total + i
    
# Output    
print("\nThe Sum of Values: ", total)
Output

Dictionary:  {'x': 200, 'y': 300, 'z': 510}

 

The Sum of Values:  1010

Program 3: Python Program to Find the Sum of Items in a Dictionary Using Another Approach

In this program, we used the for loop to iterate the dictionary and there is no python built-in function used to get a list of all items or for performing additions of all items.

Python Program to Find the Sum of Items in a Dictionary Using Another Approach
# Python Program to Find the Sum of Items in a Dictionary Using Another Approach

# Declares a Dictionary
myDict = {'x': 200, 'y':300, 'z':510}

# Print the Dictionary
print("Dictionary: ", myDict)
total = 0

# Print Values
for i in myDict:
    total = total + myDict[i]
    
# Output    
print("\nThe Sum of Values: ", total)
Output

Dictionary:  {'x': 200, 'y': 300, 'z': 510}

 

The Sum of Values:  1010


Related Posts



Comments

Recent Posts
Tags