invert dictionary python
inv_map = {v: k for k, v in my_map.items()}
invert dictionary python
inv_map = {v: k for k, v in my_map.items()}
reverse key order dict python
>>> a = {0:'000000',1:'11111',3:'333333',4:'444444'}
>>> a.keys()
[0, 1, 3, 4]
>>> sorted(a.keys())
[0, 1, 3, 4]
>>> reversed(sorted(a.keys()))
<listreverseiterator object at 0x02B0DB70>
>>> list(reversed(sorted(a.keys())))
[4, 3, 1, 0]
invert dictionary python
orig = { 1:'A', 2:'B', 3:'C' }
new = dict(zip(orig.values(), orig.keys()))
new == {'A': 1, 'B': 2, 'C': 3} #True
how to reverse a dictionary in python
def inverse_dict(my_dict):
"""
the func get a dictinary and reverse it, the keys become values and the values become keys.
:param my_dict: the dictinary that need to be reversed.
:return: a VERY pretty dictionary.
"""
result_dict = {}
for key, value in my_dict.items():
if not value in result_dict.keys():
result_dict[value] = []
result_dict[value].append(key)
return result_dict, print(result_dict)
reverse python dictionary
inv_map = {v: k for k, v in my_map.iteritems()}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us