In this post, we are going to learn how to sort dictionary By value in Python in ascending and descending order with code examples. To perform sorting we are going to use the sorted() inbuilt Python method. The dictionary is an unordered collection in python to store data in key-value pairs.
1. Sort dictionary by value Ascending Order using Lambda
The Python inbuilt sorted() method of data structure(list, tuple, dictionary string) returns a new sorted data structure object.
Syntax for sorted() Method
sorted(iterable,key_function,reverse)
- iterable: required parameter, represents the object that we want to sort.
- Key_function: It is an optional parameter, It allows to pass a function to custom sorting.
- Reverse: This is an optional parameter, We use this parameter when sorting the dictionary in descending order.
Code Example
- In this example, the object is animal_dict and the key function is lambda. The sorted method returns a sorted list of tuples by value.
- Converting a sorted list of tuples into a dictionary using dict() method.
#Program to sort a dictionary by value ascending order
animal_dict = { 'rat':30,'cat':10,'rab':15,'snack':0 }
#lambda with a sorted() method to sort a dictionary by value
sorted_dict = dict(sorted(animal_dict.items(), key=lambda item: item[1]))
print("Sorted Dictionary by value ascending order:\n ", sorted_dict)
Output
Sorted Dictionary by value ascending order:
{'snack': 0, 'cat': 10, 'rab': 15, 'rat': 30}
2. List Comprehension to sort dictionary by value
In this code example, the sorted method is used with a List comprehension.
- In this example the object is animal_dict and the key function is lambda. The sorted method returns a sorted list of tuples by value.
- Converting a sorted list of tuples into a dictionary using dict() method.
Code Example
animal_dict = { 'rat':30,'cat':10,'rab':15,'snack':0 }
#dict() to convert list to dictionary
sorted_dict_by_Val = dict(sorted((value, key) for (key,value) in animal_dict.items()))
print("Sorted Dictionary by value ascending order:\n ", sorted_dict_by_Val)
Output
Sorted Dictionary by value ascending order:
{0: 'snack', 10: 'cat', 15: 'rab', 30: 'rat'}
3. Sort Dictionary by value using orderDict
The orderDict is a subclass of the Dictionary class. The main difference between orderDict and dictionary is that the orderDict remembers the insertion order of data.
Code Example
#Program to sort a dictionary by value ascending order
from collections import OrderedDict
animal_dict = { 'rat':30,'cat':10,'rab':15,'snack':0 }
sorted_dict_by_Val = OrderedDict(sorted(animal_dict.items(), key=lambda x: x[1]))
print("Sorted Dictionary by value ascending order:\n ", sorted_dict_by_Val)
Output
Sorted Dictionary by value ascending order:
OrderedDict([('snack', 0), ('cat', 10), ('rab', 15), ('rat', 30)])
4. Sort Dictionary by value using operator Module
Itemgetter() method is defined in the operator Module. This returns a callable object that returns an item from the object using Itemgetter() method.
Code Example
#Program to sort a dictionary by value in ascending order
import operator
animal_dict = { 'rat':30,'cat':10,'rab':15,'snack':0 }
sorted_dict_by_Val = sorted(animal_dict.items(), key=operator.itemgetter(1))
list_of_tup_to_list = dict(sorted_dict_by_Val)
print("Sorted Dictionary by value ascending order:\n ", sorted_dict_by_Val)
print('list of tuples to dict:\n',list_of_tup_to_list)
Output
Sorted Dictionary by value ascending order:
[('snack', 0), ('cat', 10), ('rab', 15), ('rat', 30)]
list of tuples to dict:
{'snack': 0, 'cat': 10, 'rab': 15, 'rat': 30}
5.Sort a dictionary in Descending order
In this code example, sorting of the dictionary is performed in descending/reverse order.
Code Example
- In this example the object is animal_dict and the key function is lambda.
- Passing parameter Reverse= True, to sort dictionary in descending order.
- The sorted method returns a sorted list of tuples by value in descending order.
- Converting a sorted list of tuples into a dictionary using dict() method.
#Program to sort a dictionary by value in Descending order
animal_dict = { 'rat':30,'cat':10,'rab':15,'snack':0 }
#lambda with a sorted() method to sort a dictionary by value
sorted_dict = dict(sorted(animal_dict.items(), key=lambda item: item[1],reverse=True))
print("Sorted Dictionary by value Descending order:\n ", sorted_dict)
Output
Sorted Dictionary by value Descending order:
{'rat': 30, 'rab': 15, 'cat': 10, 'snack': 0}
Conclusion
In this post,We have understood different ways to sort a dictionary by value ascending or descending order.