I've written a one-liner to accomplish this:
vocab_tag = dict(zip(*reversed(zip(*tag_vocab.items()))))
Can anybody write one that's more comprehensible/direct?
A readable and short dict comprehension is probably the best one can do:
vocab_tage = {value: key for key, value in tag_vocab.items()}
Pre 2.7, dictionary comprehensions don't exist, but we can replace them trivially with dict()
and a generator expression:
vocab_tage = dict((value, key) for key, value in tag_vocab.items())
It's worth noting that this presumes there are no keys with the same value (as does the example in the question).
Considering time performance, dictionary comprehension is the best solution. It is both readable and performant.
Given a dictionary, a
, defined as
a = {'1' : 'a', '2' : 'b', '3' : 'c', '4' : 'd'}
the implementations perform in the following manner:
%%timeit
b = dict(zip(*reversed(zip(*a.items()))))
100000 loops, best of 3: 3.09 µs per loop
%%timeit
c = {v: k for k, v in a.items()}
1000000 loops, best of 3: 776 ns per loop
%%timeit
d = dict(map(reversed, a.items()))
100000 loops, best of 3: 4.38 µs per loop
.iteritems()
methodIf using < Python 3, .iteritems()
will perform slightly faster and is more memory efficient.
%%timeit
e = {v: k for k, v in a.iteritems()}
1000000 loops, best of 3: 598 ns per loop