Suppose there is a dictionary
a = {'a':122,'b':123,'d':333,'e':'233'}
Now I want to revert it as its a one-to-one dictionary so we can do that.
What I have tried:
In [67]: ivd=[(v,k) for (k,v) in a.items()]
In [68]: ivd
Out[68]: [(122, 'a'), (123, 'b'), ('233', 'e'), (333, 'd')]
Now may be some how We can convert this ivd into a dictionary. My questions are
ivd into a dictionary ?You can use dict constructor:
ivd = dict((v, k) for (k, v) in a.iteritems())
or dict comprehension in python 2.7 or later:
ivd = {v: k for (k, v) in a.items()}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With