Sort Dictionary By value Asec/Desc order in python

Python Dictionary

In this post, we are going to learn different Sort Dictionary By value Asec/Desc order in python 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.

Sort Dictionary By value in Ascending Order

1. Sort a dictionary by value using Lambda

The sorted() inbuilt method of Python sorts the iterable(list, tuple, dictionary string) and returns a new sorted iterable 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 and Sorted Method

In this code example,sorted method is used with 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

#Program to sort a dictionary by value ascending order

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 the 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}

Sort dictionary in descending Order by value

1.Sort a dictionary in Descending order

In this code example,sorting of dictionary is performed in descending 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.