Program to swap the values of the keys in the dictionary.

In this python dictionary program, we will swap the values of the keys in the dictionary.

Steps to solve the program
  1. Take a dictionary as input and create an empty dictionary.
  2. Use for loop to iterate over keys and values of the given dictionary using items().
  3. Swap the keys and values while adding to the other dictionary
  4. Print the output.
				
					D1 = {'name':'yash','city':'pune'}
D2 ={}

for key,value in D1.items():
    D2[value] = key
    
print(D2)
				
			

Output :

				
					{'yash': 'name', 'pune': 'city'}
				
			

concatenate two dictionaries

get the sum of all the items in a dictionary.

Leave a Comment