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
Info

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.