order dictionary by value python

import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(1))


# Sort by key
import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(0))

Here is what the above code is Doing:
1. We create a dictionary x.
2. We use sorted() to sort the dictionary by value.
3. We use operator.itemgetter(1) to specify that we are sorting by value.
4. We use operator.itemgetter(0) to specify that we are sorting by key.