I'm sorting my dictionary by key, but I'd like to reverse the order. However I'm not getting much joy with a few of the examples I have seen around the web.
Here is the sort
tempdict = collections.OrderedDict(sorted(tempdict.items()))
now I'm trying:
reverse = collections.OrderedDict(tempdict.items()[::-1])
reverse = collections.OrderedDict(map(reversed, tempdict.items()))
But these are not working. What is the smartest and most elegant way of sorting the dictionary. Yes I know, dictionaries are not really used for the sorting, but this works well for us. Thanks.
To sort in reverse order:
collections.OrderedDict(sorted(tempdict.items(), reverse=True))
To reverse an existing dict:
collections.OrderedDict(reversed(list(tempdict.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