In this python dictionary program, we will swap the values of the keys in the dictionary.
Steps to solve the program
- Take a dictionary as input and create an empty dictionary.
- Use for loop to iterate over keys and values of the given dictionary using items().
- Swap the keys and values while adding to the other dictionary
- 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'}