Swap the values of two keys

EasyDictionary Programs · 15 of 94

Write a Python program to swap the values of two keys.

Advertisement
Input
{"name": "yash", "city": "pune"}
Output
{'name': 'pune', 'city': 'yash'}

Approach

  1. Tuple assignment on the two keys.

Solution

Python
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.

More dictionary programs

← All 94 Dictionary Programs

Leave a Comment