There are various ways of doing this, some of them are a follows:
1. Inverting with map and reversed
map_inv = dict(map(reversed,map_old.items()))
2.Inverting with a comprehension
For Python 2.7.x use:
map_inv = {v: k for k, v in map_old.iteritems()}
For Python 3 and higher version use:
map_inv = {v: k for k, v in map_old.items()}
3.Inverting with a Defaultdict
from collections import defaultdict
map_inv = defaultdict(list)
{map_inv[v].append(k) for k, v in map_old.items()}
4.Inverting with a for loop:
map_inv = dict()
for key, value in map_old.items():
map_inv.setdefault(value, list()).append(key)
The preferable way is by using the dictionary comprehension but it has a constraint that it cannot work with non-unique values. So, if you are working with non-unique values then refer to the other methods.
Hope it helps!
If you are looking for upskilling yourself in python you can join our Python Certification and learn from an industry expert.