How do I sort a dictionary by key or value in Python?

You can use the built-in method itemgetter() of operator module to sort a dictionary by key or value in Python as shown in the below example –