Sort a Dictionary in Python With Examples

Introduction

In this tutorial, we are going to take a look at the various ways following which we can Sort a Dictionary in Python.

In Python, a Dictionary is a collection of key-value pairs. The sequence of such key and value pairs is separated by commas. These pairs are called items.

Different Ways to sort a Dictionary in Python

As mentioned above, a dictionary item consists of a key and its corresponding value. Hence, sorting for a dictionary can be performed by using any one of the key or value parts as a parameter.

So, let us now take a look at the different methods by which we can sort a dictionary by key or by value.

1. Sort Dictionary by Key

We can directly sort a dictionary using the built-in sorted() method in Python. This can be done by passing the dictionary itself and a function that specifies the parameter on the basis of which the sorting is to be done(in this case key).

Let us see how.

Output:

Here,

  • The d.items() method returns a list of tuples(items) containing the keys and their corresponding values,
  • The lambda function returns the key(0th element) for a specific item tuple,
  • When these are passed to the sorted() method, it returns a sorted sequence which is then type-casted into a dictionary.

Remember, this method can be used in Python 3.6+ versions as it considers dictionaries as ordered sequences. For older versions, we can replace the lambda function with the itemgetter() method from the operator module. Let us see how.

Output:

Here also the methods sorted() and items() work the same way. But in place of a lambda function, the itemgetter(0) method returns a callable object that fetches the 0th item from its operand using the operand’s __getitem__() method. In this case, as we need to sort by key, we consider the 0th element.

2. Sort a Dictionary by Value

Sorting a dictionary by value is similar to sorting by key. The only difference being that this type the parameter on the basis of which the sorting would be done is the value part of the corresponding items.

Hence as we did earlier, we can use the sorted() method along with a lambda function for Python 3.6+ versions. Let us see how.

Output:

Similarly here, according to the values returned by the lambda function(x[1] value for an item x) the dictionary d is sorted.

Again for older versions of Python follow the below-mentioned method.

Output:

Similarly, sorted() along with d.items() and itemgetter(1) methods sort the dictionary d on the basis of value.

3. Sorting in reverse order

The sorted() method comes with another argument reverse. That can be used for specifying the order in which the sorting is to be done. If passed True, the sorting takes place in reverse order(descending). And if False is passed(default), the sorting takes place in ascending order.

Let us try to understand this by an example where we try to reverse sort a dictionary by key.

Output:

From the above output, it is clear that passing the reverse parameter as True the above dictionary is sorted in reverse order(descending).

Conclusion

So in this tutorial, we learned how we can sort a dictionary in Python using various methods.

For any further questions, feel free to use the comments below.

References

By admin

Leave a Reply

%d bloggers like this: