Inverting a Dictionary Mapping
Given a dictionary that maps keys to values, there may be a need to invert the mapping, switching the roles of keys and values. Here's how this can be achieved in Python:
Solution:
Python provides a convenient method to invert a dictionary using a dictionary comprehension:
Python 3 :
inv_map = {v: k for k, v in my_map.items()}
Python 2:
inv_map = {v: k for k, v in my_map.iteritems()}
The above is the detailed content of How Can I Reverse a Dictionary's Key-Value Mapping in Python?. For more information, please follow other related articles on the PHP Chinese website!