Reverse / invert a dictionary mapping
Here's an example of how you can reverse or invert a dictionary mapping in Python:
Here's an example of how you can reverse or invert a dictionary mapping in Python:
Reverse / 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'}
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Python - The Practical Guide</div>
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.