In [12]: d = {'1': ['a','b', 'c', 'd'], '2': ['e','f', 'g', 'h']}
In [13]: dict((v[-1],v[:-1]+[k]) for k,v in d.iteritems())
Out[13]: {'d': ['a', 'b', 'c', '1'], 'h': ['e', 'f', 'g', '2']}
k
is the original key, v
is the original value (the list). In the result, v[-1]
becomes the new key and v[:-1]+[k]
becomes the new value.
edit As pointed out by Adrien Plisson, Python 3 does not support iteritems
. Also, in Python 2.7+ and 3, one can use a dictionary comprehension:
{v[-1]: v[:-1]+[k] for k,v in d.items()} # Python 2.7+