Sorting dictionary descending in Python

This should work

{k: v for k, v in sorted(dict.items(), key=lambda item: item[1], reverse = True)}

There is a reverse option to sorted() you could use instead:

sorted(dict.items(), key=lambda kv: kv[1], reverse=True)

This produces the exact same output, and even works if the values are not numeric.


Python dictionary aren't sortable. Your sorted_dictionary output is not a dictionary but a list. You have to use OrderedDict

from collections import OrderedDict

sorted_dictionary = OrderedDict(sorted(dict.items(), key=lambda v: v, reverse=True))