Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis0
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
+ 1
ubai that's what I was expected... Thank you