Dictionaries are not ordered. So there is no way to do it.
If you have python2.7+, you can use collections.OrderedDict
– in this case you could retrieve the item list using .items()
and then reverse it and create a new OrderedDict
from the reversed list:
>>> od = OrderedDict((('a', 'first'), ('b', 'second')))
>>> od
OrderedDict([('a', 'first'), ('b', 'second')])
>>> items = od.items() # list(od.items()) in Python3
>>> items.reverse()
>>> OrderedDict(items)
OrderedDict([('b', 'second'), ('a', 'first')])
If you are using an older python version you can get a backport from http://code.activestate.com/recipes/576693/