Python Practice › Dictionary Programs › Program 15
Swap the values of two keys
Write a Python program to swap the values of two keys.
Input
{"name": "yash", "city": "pune"}Output
{'name': 'pune', 'city': 'yash'}Approach
- Tuple assignment on the two keys.
Solution
d = {"name": "yash", "city": "pune"}d["name"], d["city"] = d["city"], d["name"]print(d)Output
{'name': 'pune', 'city': 'yash'}Learn the concept
New to this topic? Read the lesson, then come back.