Invert mapping of a dictionary in Python
This post will discuss how to invert the mapping of a dictionary in Python, where all values in the dictionary are unique. The dictionary { k1: v1, k2: v2, … , kn: vn} should be transformed into a dictionary { v1: k1, v2: k2, … , vn: kn}.
1. Using Dictionary Comprehension
A simple solution to invert each key-value pair of a dictionary is using dictionary comprehension, which allows us to create a new dictionary by applying some condition on each key-value pair of an existing dictionary. Here is how we can use dictionary comprehension to invert mapping of a dictionary:
|
1 2 3 4 |
d = {'A': 1, 'B': 2, 'C': 3} inverse_dict = {v: k for k, v in d.items()} print(inverse_dict) # {1: 'A', 2: 'B', 3: 'C'} |
This method is concise and elegant, and works for any type of key-value pair. We can also use a dictionary constructor instead of dictionary comprehension. The dictionary constructor takes an iterable object (such as a list or a tuple) of key-value pairs as an argument and returns a new dictionary with those key-value pairs.
|
1 2 3 4 |
d = {'A': 1, 'B': 2, 'C': 3} inverse_dict = dict((v, k) for k, v in d.items()) print(inverse_dict) # {1: 'A', 2: 'B', 3: 'C'} |
2. Using Dictionary Iterators
The fastest solution is to use dictionary iterators to iterate over the dictionary’s keys and create a new dictionary with reverse mapping. Here is how we can use dictionary iterators to invert mapping of a dictionary:
|
1 2 3 4 |
d = {'A': 1, 'B': 2, 'C': 3} inverse_dict = {d[k]: k for k in d} print(inverse_dict) # {1: 'A', 2: 'B', 3: 'C'} |
3. Using map() and reversed() functions
Another way to invert mapping of a dictionary in Python is using the map() and reversed() functions. The map() function provides a convenient way to apply a function to every item of an iterable and the reversed() function returns an iterator that yields the items in reverse order. They can be used as follows to inverse a dictionary:
|
1 2 3 4 |
d = {'A': 1, 'B': 2, 'C': 3} inverse_dict = dict(map(reversed, d.items())) print(inverse_dict) # {1: 'A', 2: 'B', 3: 'C'} |
If we need to modify the dictionary in-place rather than creating a new dictionary, we can create a copy of the dictionary and update it with value-key pairs. Here is an example of how to use these functions to create an inverse dictionary:
|
1 2 3 4 5 6 7 |
d = {'A': 1, 'B': 2, 'C': 3} ref = d.copy() d.clear() d.update(map(reversed, ref.items())) print(d) |
4. Using zip() function
A final way to invert mapping of a dictionary in Python is to use the zip() function with the dictionary constructor. The zip() function takes two or more iterable objects and yields tuples containing corresponding items from each iterable. Here is an example of its usage to invert mapping of a dictionary:
|
1 2 3 4 |
d = {'A': 1, 'B': 2, 'C': 3} inverse_dict = dict(zip(d.values(), d.keys())) print(inverse_dict) # {1: 'A', 2: 'B', 3: 'C'} |
How to handle duplicates?
If the dictionary’s values aren’t unique, we can use a simple for-loop to handle duplicate values in a dictionary. This is demonstrated below:
|
1 2 3 4 5 6 7 |
d = {'A': 1, 'B': 2, 'C': 2} inverse_dict = {} for k, v in d.items(): inverse_dict.setdefault(v, []).append(k) print(inverse_dict) # {1: ['A'], 2: ['B', 'C']} |
That’s all about inverting the mapping of a dictionary in Python.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)