0
How to replace value with key in dictionary?
For example: a={1:'a',2:'b'} Now I want a new dictionary: a={'a':1,'b':2}
5 Respostas
+ 3
a={y:x for x,y in a.items()}
+ 2
Amirreza Hashemi
What if there are duplicate values? In this case when you swap then new key will replace old key.
For example:
a = {1: 'a', 2 : 'b', 3 : 'b'}
In this case result will be
a = {a : 1, b : 3}
So to avoid this above solutions are ok.
+ 1
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ thanks
+ 1
ubai that's what I was expected... Thank you