Reverse / invert a dictionary mapping
Here's an example of how you can reverse or invert a dictionary mapping in Python:
original_dict = {'a': 1, 'b': 2, 'c': 3}
inverted_dict = dict(map(reversed, original_dict.items()))
print(inverted_dict)
# Output: {1: 'a', 2: 'b', 3: 'c'}
Watch a video course
Python - The Practical Guide
In this example, original_dict
is the dictionary that you want to invert. The map()
function is used to apply the reversed()
function to each item in the dictionary, which returns a tuple of the form (value, key). This tuple is then passed to the dict()
constructor to create the inverted dictionary.