In this article we will discuss how to sort the contents of dictionary by key or value.

Suppose we have a dictionary of string and ints i.e.

Dictionary is like a hash table that store the elements by calculating hashes of keys and orders of elements in it can not be predicted. Therefore, its also called unordered container and we can sort the dictionary in place. But we can create a either create a list of tuples (key value) pairs which is sorted or we can iterate over the contents of dictionary in sorted order.

Let’s see how to do sort the contents of dictionary in different ways,

Sort a dictionary contents by key

There are different ways to sort the elements of dictionary be keys i.e.

Sort Dictionary contents by keys using dict.keys()

It returns a iterable view of all the keys in dictionary.

We can create a new sorted dictionary from this iterable sequence of keys i.e.

Now we will iterate over this sorted list of keys and select each entry from dictionary i.e.

It will print the dictionary in a sorted order of keys i.e.

Now we can achieve the same thing using an another function of dictionary i.e. items(). Its more efficient.

Sort Dictionary contents by keys using dict.items()

It returns an iterable sequence of tuples that contains all key value pairs in dictionary.
We can create a new sorted list of tuple by this iterable sequence i.e.

By default sorted will sort the list of tuple by 1st element in tuple i.e. on 0th index. So list of tuples (key / value pairs) is sorted by keys. Now we can iterate over this sort list of tuple i.e. all sorted keys value pairs from dictionary i.e.

It will print the dictionary in a sorted order of keys i.e.

It’s more efficient than previous method in terms of complexity, because after sorting the iterable sequence we don’t need to search for value for key like in case of dict.keys().

We can achieve the same using List Comprehension i.e.

Sorting dictionary contents in reverse order of keys

Both the previous solutions sorted the dictionary by key but in ascending order. What if we want to sort the contents by descending order of keys. We can so this by simply passing an attribute in sorted() function i.e. reverse=True i.e.

Let’s use this to sort the list of tuples by keys (0th index element) and in reverse order i.e. descending order,

It will print the dictionary in a sorted order of keys in reverse i.e.

Sort dictionary contents by key using custom key functions

We can also sort the contents of dictionary by custom logic. Like in case our dictionary we want to sort by the length of key strings.

sorted() function accepts a key function as an argument and calls it on each element prior to make comparison with other elements.
So, to sort the dictionary keys by length of string, we will pass a lambda function as key function that will return the size of string i.e.

It will create a list of tuples, sorted by key size i.e.

Sort dictionary contents by Value

To sort dictionary elements by value we will use the same sorted() function and pass a key function that will return the 1th index element of tuple i.e. the value field from the key/value pair,

As this key function will be called on each element before making comparison while sorting, so the list of tuples will be sorted by value i.e.

We can do the same using List comprehension too,

Sorting dictionary by value in reverse Order

Output:

Complete example is as follows,

Output:

 

Click Here to Subscribe for more Articles / Tutorials like this.