Python : Sort a dictionary based on value and key of items

Sorting a dictionary in ascending order based on the value of the item.

The idea is to use:

  • sorted function. The signature of sorted funciton is sorted (iterable, key = None, reverse = False)
    • sorted function would return the sorted list generated from the items in iterable
    • key specifies a function that key single argument for extracting a comparison key from each element in iterable.
    • reverse when set to true would sort the list in descending order.
  • lambda, this keyword could be used to create mini anonymous functions for specifying the key in sorted function.

Thus,

  • iterable would be _dict.items ( ). The item returned using items ( ) function would be a tuple of key and value.
    i.e key would be item [ 0 ] & value would be item [ 1 ].
  • key would be lambda item : item [ 1 ] as we are sorting based on the values


Python3 : Sorting a dictionary by value in ascending order

_dict = { 'Mango': 100, 'Kiwi' : 50, 'Avacado' : 60, 'Pear' : 25, 'Coconut' : 42, 'Plum' : 48 }

_dict = dict(sorted(_dict.items(), key=lambda item: item[1]))

for item in _dict.items():
    print ("Key : " + item[0] + ", Value : " + str(item[1]))

Output

Key : Pear, Value : 25
Key : Coconut, Value : 42
Key : Plum, Value : 48
Key : Kiwi, Value : 50
Key : Avacado, Value : 60
Key : Mango, Value : 100

To sort the dictionary by value in descending order, we would set the reverse=True in the sorted function.

Python3 : Sorting a dictionary by value in descending order

_dict = { 'Alfa': 60, 'Beta' : 76, 'Gamma' : 90, 'Delta' : 48 }

_dict = dict(sorted(_dict.items(), key=lambda item: item[1], reverse=True))

for item in _dict.items():
    print ("Key : " + item[0] + ", Value : " + str(item[1]))

Output

Key : Gamma, Value : 90
Key : Beta, Value : 76
Key : Alfa, Value : 60
Key : Delta, Value : 48


To sort the dictionary in ascending order based on keys, we would use item[ 0 ] in the lambda function.

Python3 : Sorting a dictionary by key in ascending order

_dict = { 'Craft': 10, 'Art' : 6, 'Talent' : 50, 'Skill' : 48, 'Luck': 100 }

_dict = dict(sorted(_dict.items(), key=lambda item: item[0]))

for item in _dict.items():
    print ("Key : " + item[0] + ", Value : " + str(item[1]))

Output

Key : Art, Value : 6
Key : Craft, Value : 10
Key : Luck, Value : 100
Key : Skill, Value : 48
Key : Talent, Value : 50


To sort the dictionary in descending order based on keys, we would use item[ 0 ] in the lambda function and set the reverse=True in the sorted function.

Python3 : Sorting a dictionary by key in descending order

_dict = { 'D': 1, 'Z' : 10, 'T' : 5, 'S' : 8, 'A': 0 }

_dict = dict(sorted(_dict.items(), key=lambda item: item[0], reverse=True))

for item in _dict.items():
    print ("Key : " + item[0] + ", Value : " + str(item[1]))

Output

Key : Z, Value : 10
Key : T, Value : 5
Key : S, Value : 8
Key : D, Value : 1
Key : A, Value : 0


Copyright (c) 2019-2021, Algotree.org.
All rights reserved.