Get the sum of all the items in a dictionary.

In this python dictionary program, we will get the sum of all the items in a dictionary.

Steps to solve the program
  1. Take a dictionary as input and create a variable named total and assign its value equal to 0.
  2. Use for loop to iterate over all the values in the dictionary using values().
  3. Print the output. 
				
					D1 =  {'x':23,'y':10,'z':7}
total = 0
for val in D1.values():
    total += val
    
print(total)
				
			

Output :

				
					40
				
			

Swap the values of the keys in the dictionary.

get the size of a dictionary in python.

Leave a Comment