dict = {
'one' : 8,
'two' : 1,
'three' : 12
}
{'one': 8, 'three': 12, 'two': 1}
import operator
sorted_x = sorted(dict.items(), key=operator.itemgetter(1))
[('two', 1), ('one', 8), ('three', 12)]
list
sorted_dict = collections.OrderedDict(dict)
OrderedDict([('one', 8), ('two', 1), ('three', 12)])
# Reverse sort
reverse_dict= sorted(dict.items(), key=lambda x: x[1], reverse=True)
[('three', 12), ('one', 8), ('two', 1)]
for x in reverse_dict:
print(x[0], "==>", x[1])
three ==> 12
one ==> 8
two ==> 1