KnowledgeBoat Logo
|

Computer Science

Given the dictionary x = {'k1':'v1', 'k2':'v2', 'k3':'v3'}, create a dictionary with the opposite mapping, i.e., write a program to create the dictionary as :
inverted_x = {'v1': 'k1' , 'v2' :'k2' , 'v3':'k3'}

Python

Python Dictionaries

17 Likes

Answer

x = { "k1" : "v1" , "k2" : "v2", "k3" : "v3"}
inverted_x = {}
for i in x :
    inverted_x[x[i]] = i
print(inverted_x)

Output

{'v1': 'k1', 'v2': 'k2', 'v3': 'k3'}

Answered By

9 Likes


Related Questions