A dictionary in Python is a data structure which stores values as a key-value pair. We can sort this type of data by either the key or the value and this is done by using the sorted()
function.
First, we need to know how to retrieve data from a dictionary to be passed on to this function.
There are two basic ways to get data from a dictionary:
This method takes one mandatory and two optional arguments:
true
will sort the list in descending order. Leaving this empty sorts in ascending order.Depending on the data we pass on to the sorted()
function, we have three different ways to sort a Dictionary in Python.