How to sort a Dictionary by value in Python

In this tutorial, we will learn how to sort a Dictionary by value in Python.

The values in a Python dictionary can be of any data type and can be repeated too. However, the keys in a Python dictionary can not be repeated.
An example for a Python dictionary is as follows:

eg_dic={1:"apple",2:"banana"}
print("The dictionary is:",eg_dic)
The dictionary is: {1: 'apple', 2: 'banana'}

In this tutorial, you will learn how to sort the dictionary items by value, in Python.

By using the for loop: sort a Dictionary by value in Python

You can loop through the items in the dictionary and sort them in order. However, you must note that sorting doesn’t allow to re-order an existing dictionary. So, you will have to write the ordered items into a new, empty dictionary.
An example for the same is shown below:

eg_dic={1:1,2:3,3:2}
print("The dictionary before sorting:",eg_dic)
after_sorting=sorted(eg_dic.values())
dic_after_sorting={}
for i in after_sorting:
    for j in eg_dic.keys():
        if eg_dic[j]==i:
             dic_after_sorting[j]=eg_dic[j]
print("The dictionary after sorting:",dic_after_sorting)
The dictionary before sorting: {1: 1, 2: 3, 3: 2}
The dictionary after sorting: {1: 1, 3: 2, 2: 3}

In the above code, eg_dic.values() sorts the items in the dictionary by their values.

By using the sorted() function

The sorted() method can be used to reduce the amount of code to be written, in comparison to the looping method. Here, you pass an extra key argument to the sorted() function.

eg_dic={1:1,2:3,3:2}
dic_after_sorting={}
after_sorting=sorted(eg_dic,key=eg_dic.get)
for i in after_sorting:
    dic_after_sorting[i]=eg_dic[i]
print(dic_after_sorting)
{1: 1, 3: 2, 2: 3}

The key in key=eg_dic.get calls for each element of the list. The get in key=eg_dic.get gets the value of each key element. Thus, it will return a list of keys with the values in sorted order. Using this, you can create a new sorted dictionary.

Using the lambda(λ) function: sort a Dictionary by value in Python

The lambda function is a nameless, anonymous function that you can use to get the value of a dictionary item. The items() return the key-value pairs of a dictionary as a list of tuples.

eg_dic={1:1,2:3,3:2}
print("The dictionary before sorting:",eg_dic)
after_sorting=sorted(eg_dic.items(),key=lambda item:item[1])
print(after_sorting)
dic_after_sorting={k:v for k,v in after_sorting}
print(dic_after_sorting)
The dictionary before sorting: {1: 1, 2: 3, 3: 2}
[(1, 1), (3, 2), (2, 3)]
{1: 1, 3: 2, 2: 3}

The key=lambda item:item[] returns the value of each key value pair and the sorted() sorts these items.

OrderedDict of the collections module: sort a Dictionary by value in Python

For Python versions before 3.7, you can use the OrderedDict method. The itemgetter() is a method from the operator class, that will return an item from the callable object that it returns.

import operator
from collections import OrderedDict
eg_dic={1:1,2:3,3:2}
print("The dictionary before sorting:",eg_dic)
after_sorting=sorted(eg_dic.items(),key=operator.itemgetter(1))
print(after_sorting)
dic_after_sorting=OrderedDict()
for k,v in after_sorting:
    dic_after_sorting[k]=v
print("The dictionary after sorting",dic_after_sorting)
The dictionary before sorting: {1: 1, 2: 3, 3: 2}
[(1, 1), (3, 2), (2, 3)]
The dictionary after sorting OrderedDict([(1, 1), (3, 2), (2, 3)])

Learn more, List and Dictionary Manipulation in Python

Leave a Reply

Your email address will not be published. Required fields are marked *