Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. Inverting a dictionary means swapping keys and values, so the original values become the new keys and vice versa.
Using Dictionary Comprehension
The most Pythonic way to invert a dictionary is using dictionary comprehension ?
# initialising dictionary
ini_dict = {101: "vishesh", 201: "laptop"}
# print initial dictionary
print("initial dictionary:", 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:", inv_dict)
initial dictionary: {101: 'vishesh', 201: 'laptop'}
inverse mapped dictionary: {'vishesh': 101, 'laptop': 201}
Using zip() and dict() Functions
The zip() function pairs values with keys, then dict() creates the inverted dictionary ?
# initialising dictionary
ini_dict = {101: "vishesh", 201: "laptop"}
# print initial dictionary
print("initial dictionary:", 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:", inv_dict)
initial dictionary: {101: 'vishesh', 201: 'laptop'}
inverse mapped dictionary: {'vishesh': 101, 'laptop': 201}
Using map() and reversed()
The map() function applies reversed() to each key-value tuple, swapping their positions ?
# initialising dictionary
ini_dict = {101: "akshat", 201: "ball"}
# print initial dictionary
print("initial dictionary:", ini_dict)
# inverse mapping using map and reversed
inv_dict = dict(map(reversed, ini_dict.items()))
# print final dictionary
print("inverse mapped dictionary:", inv_dict)
initial dictionary: {101: 'akshat', 201: 'ball'}
inverse mapped dictionary: {'akshat': 101, 'ball': 201}
Using Lambda Function
You can create a lambda function that returns an inverted dictionary ?
# initialising dictionary
ini_dict = {101: "akshat", 201: "ball"}
# print initial dictionary
print("initial dictionary:", ini_dict)
# inverse mapping using lambda
invert_dict = lambda d: {v: k for k, v in d.items()}
inv_dict = invert_dict(ini_dict)
# print final dictionary
print("inverse mapped dictionary:", inv_dict)
initial dictionary: {101: 'akshat', 201: 'ball'}
inverse mapped dictionary: {'akshat': 101, 'ball': 201}
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| Dict Comprehension | High | Fast | Most cases |
| zip() + dict() | Medium | Fast | Functional style |
| map() + reversed() | Low | Medium | Educational purposes |
| Lambda | Medium | Fast | Reusable function |
Conclusion
Dictionary comprehension is the most readable and efficient method for inverting dictionaries. Use zip() with dict() for functional programming style, and lambda functions when you need a reusable inversion function.
