Python | Ways to invert mapping of dictionary

Dictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. It is widely used in day to day programming, web development, and machine learning.
Let’s discuss a few ways to invert mapping of a dictionary.
Method #1: Using Dictionary Comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# how to invert mapping 
# using dict comprehension
  
# initialising dictionary
ini_dict = {101: "akshat", 201 : "ball"}
  
# print initial dictionary
print("initial dictionary : ", str(ini_dict))
  
# inverse mapping using dict comprehension
inv_dict = {v: k for k, v in ini_dict.items()}
  
# print final dictionary
print("inverse mapped dictionary : ", str(inv_dict))

chevron_right


Output:

initial dictionary :  {201: 'ball', 101: 'akshat'}
inverse mapped dictionary :  {'ball': 201, 'akshat': 101}

Method #2: Using dict.keys() and dict.values()

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# how to invert mapping 
# using zip and dict functions
  
# initialising dictionary
ini_dict = {101: "akshat", 201 : "ball"}
  
# print initial dictionary
print("initial dictionary : ", str(ini_dict))
  
# inverse mapping using zip and dict functions
inv_dict = dict(zip(ini_dict.values(), ini_dict.keys()))
  
# print final dictionary
print("inverse mapped dictionary : ", str(inv_dict))

chevron_right


Output:

initial dictionary :  {201: 'ball', 101: 'akshat'}
inverse mapped dictionary :  {'ball': 201, 'akshat': 101}

Method #3: Using map() and reversed



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# how to invert mapping 
# using map and reversed
  
# initialising dictionary
ini_dict = {101: "akshat", 201 : "ball"}
  
# print initial dictionary
print("initial dictionary : ", str(ini_dict))
  
# inverse mapping using map and reversed
inv_dict = dict(map(reversed, ini_dict.items()))
  
# print final dictionary
print("inverse mapped dictionary : ", str(inv_dict))

chevron_right


Output:

initial dictionary :  {201: 'ball', 101: 'akshat'}
inverse mapped dictionary :  {'akshat': 101, 'ball': 201}

Method #4: Using lambda

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# how to invert mapping 
# using lambda
  
# initialising dictionary
ini_dict = {101 : "akshat", 201 : "ball"}
  
# print initial dictionary
print("initial dictionary : ", str(ini_dict))
  
# inverse mapping using lambda
lambda ini_dict: {v:k for k, v in ini_dict.items()}
  
# print final dictionary
print("inverse mapped dictionary : ", str(ini_dict))

chevron_right


Output:

initial dictionary :  {201: 'ball', 101: 'akshat'}
inverse mapped dictionary :  {201: 'ball', 101: 'akshat'}


My Personal Notes arrow_drop_up

Programming freak_tech Enthusiast and have interest in learning new upcoming technologies.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.