Sort by dictionary elements (which are tuples) using sorted() . You can specify the sort key, which will have the values โโof the dictionary, and then its keys:
>>> d = {'Bill': 4, 'Alex' : 4, 'Bob' : 3, "Charles": 7} >>> sorted(d.items(), key=lambda x:(x[1],x[0])) [('Bob', 3), ('Alex', 4), ('Bill', 4), ('Charles', 7)] >>> [t[0] for t in sorted(d.items(), key=lambda x:(x[1],x[0]))] ['Bob', 'Alex', 'Bill', 'Charles']
source share