192k views
0 votes
In python:

Given a dictionary d, create a new dictionary that reverses the keys and values of d. Thus, the keys of d become the values of the new dictionary and the values of d become the keys of the new dictionary. You may assume d contains no duplicate values (that is, no two keys map to the same values.) Associate the new dictionary with the variable inverse.

User Fsquirrel
by
5.3k points

1 Answer

2 votes

Answer:

Following are the program in the Python Programming Language:

#set dictionary

d = { 'State' : 'Delhi', 'Country' : 'India'};

#reverse the dictionary.

revers = dict((v, k) for k, v in d.items())

#Displays the reversed dictionary.

print("Dictionary is: \\")

print(revers)

Output:

Dictionary is:

{'Delhi': 'State', 'India': 'Country'}

Step-by-step explanation:

Here, we set the dictionary data type variable "d" then, we set the variable in which we store the reverse of the dictionary in which key is converted into the value and value is converted into the key. and finally we print the reverse dictionary.

User Mweber
by
5.0k points