- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Example
# using dict comprehension # initialising dictionary ini_dict = {101: "vishesh", 201 : "laptop"} # 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)) # using zip and dict functions # initialising dictionary ini_dict = {101: "vishesh", 201 : "laptop"} # 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)) # 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)) # 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))
Output
('initial dictionary : ', "{201: 'laptop', 101: 'vishesh'}") ('inverse mapped dictionary : ', "{'laptop': 201, 'vishesh': 101}") ('initial dictionary : ', "{201: 'laptop', 101: 'vishesh'}") ('inverse mapped dictionary : ', "{'laptop': 201, 'vishesh': 101}") ('initial dictionary : ', "{201: 'ball', 101: 'akshat'}") ('inverse mapped dictionary : ', "{'ball': 201, 'akshat': 101}") ('initial dictionary : ', "{201: 'ball', 101: 'akshat'}") ('inverse mapped dictionary : ', "{201: 'ball', 101: 'akshat'}")
- Related Articles
- Python – Mapping Matrix with Dictionary
- Python – Cross mapping of Two dictionary value lists
- Python - Ways to Copy Dictionary
- Python - Ways to create a dictionary of Lists
- Python - Ways to remove a key from dictionary
- Python Mapping Types
- Program to find number of ways to form a target string given a dictionary in Python
- Invert Binary Tree in Python
- Program to invert a binary tree in Python
- Flip and Invert Matrix in Python
- How to invert a matrix or nArray in Python?
- How to convert Javascript dictionary to Python dictionary?
- Convert string dictionary to dictionary in Python
- How to create Python dictionary from the value of another dictionary?
- Decrypt String from Alphabet to Integer Mapping in Python

Advertisements