Dicionário invertido Python
inv_map = {v: k for k, v in my_map.items()}
Busy Boar
inv_map = {v: k for k, v in my_map.items()}
inv_map = {v: k for k, v in my_map.iteritems()}
inv_map = {v: k for k, v in my_map.iteritems()}
def inverse_dict(my_dict):
"""The function accepts a dictionary as a parameter.
The function returns a new dictionary with "inverted" mapping:
each key in the transmitted dictionary is a value in the returned dictionary
and value entry in the transmitted dictionary is a key in the returned dictionary.
:param my_dict: dictionary
:type my_str: string
:return: Dictionary
:rtype: Dictionary
"""
new_dict = {} # create empty Dictionary
for itm1 in my_dict.items():
lst = []
for itm2 in my_dict.items():
if itm2[1] == itm1[1]:
lst.append(itm2[0])
new_dict[itm1[1]] = sorted(lst)
return new_dict