sort dictionary by value python without sorted code example
Example 1: Python sort dictionary by value
from operator import itemgetter
new_dict = sorted(data.items(), key=itemgetter(1))
Example 2: sort a dictionary by value then key
d = {'apple': 2, 'banana': 3, 'almond':2 , 'beetroot': 3, 'peach': 4}
[v[0] for v in sorted(d.items(), key=lambda kv: (-kv[1], kv[0]))]