Sort a python dictionary by value

Note that you can’t have a sorted dictionary; you can just make a list of the sorted tuples.

import operator

dishes = {'spam': 6, 'eggs': 2, 'bacon': 3, 'tomatoes': 1}
sorted_dishes = sorted(dishes.items(), key=operator.itemgetter(1))