W3docs

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

python— editable, runs on the server

<div class="alert alert-info flex not-prose"> Watch a course <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.