In this article we will discuss different ways to sort a python dictionary by values in both ascending & descending order using sorted() function by either using lambda function or itemgetter() as a key argument.
Table of Contents
- Overview of Sorted() function in python
- Sort the dictionary by value using sorted() & lambda function in python
- Sort the dictionary by value in descending order using lambda function in python
- Sort the dictionary by value using sorted() & itemgetter in python
- Sort the dictionary by value in descending order using itemgetter in python
Suppose we have a dictionary,
word_dict = { 'this': 11, 'at': 9, 'here': 5, 'why': 12, 'is': 2, }
Now we want to sort this dictionary based on values. Sorted version should be like,
{'is': 2, 'here': 5, 'at': 9, 'this': 11, 'why': 12}
To do this, we will use the sorted() function. Let’s first have a little overview of the sorted() function,
Overview of Sorted() function in python
Python provides a function to sort items in an iterable sequence i.e.
sorted(iterable, key=key, reverse=reverse)
- It accepts an iterable sequence as the first argument and returns a sorted list of items in that sequence.
- By default, it uses the < operator to compare elements in the sequence, but if a key argument (A call-back function) is provided, then it uses that to compare the items in the sequence while sorting.
- The default value of the reverse argument is True; therefore it sorts the items in ascending order by default. To sort them in descending order, provide the reverse argument as False.
Dictionary in python is also an iterable sequence of key-value pairs. So, to sort a dictionary we can use the sorted() function,
Frequently Asked:
Sort the dictionary by value using sorted() & lambda function in python
If we pass our dictionary to the sorted() function, without any other argument. Then sorted() function will sort the key-value pairs in the dictionary by comparing them using < operator i.e. by default items in the dictionary will be sorted by key. But we want to sort the items by value instead. For this we need to pass the key function as an argument to the sorted function i.e.
word_dict = { 'this': 11, 'at': 9, 'here': 5, 'why': 12, 'is': 2 } # Sort Dictionary by value in descending order using lambda function sorted_dict = dict( sorted(word_dict.items(), key=lambda item: item[1], reverse=True)) print('Sorted Dictionary: ') print(sorted_dict)
Output:
Sorted Dictionary: {'is': 2, 'here': 5, 'at': 9, 'this': 11, 'why': 12}
It returned a new dictionary, which is sorted by value.
But how did it work?
It sorted the key-value pairs in the dictionary by comparing them using a comparator passed as a key argument. In our case we passed a lambda function that, accepts a key-value pair as an argument and returns the value field from that pair. Now while sorting items in the dictionary, sorted() function passed each key-value pair to this given lambda function and used the returned value to compare the items. Therefore our dictionary is sorted by value.
Sort the dictionary by value in descending order using lambda function in python
In addition to the lambda function like in the previous example, also pass the reverse argument as True to the sorted() function. It will sort the items in the dictionary by value, but in descending order i.e.
word_dict = { 'this': 11, 'at': 9, 'here': 5, 'why': 12, 'is': 2 } # Sort Dictionary by value in descending order using lambda function sorted_dict = dict( sorted(word_dict.items(), key=lambda item: item[1], reverse=True)) print('Sorted Dictionary: ') print(sorted_dict)
Output:
{'why': 12, 'this': 11, 'at': 9, 'here': 5, 'is': 2}
Sort the dictionary by value using sorted() & itemgetter in python
Python’s operator module provides an itemgetter, whose object fetches the given item from its operands. It means,
- itemgetter(2)(r) returns r[2]
- itemgetter(1)(r) returns r[1].
We can pass the itemgetter object as a key argument in the sorted() function, to fetch the value field from each key-value item of the dictionary while sorting i.e.
import operator word_dict = { 'this': 11, 'at': 9, 'here': 5, 'why': 12, 'is': 2 } # Sort Dictionary by value using itemgetter sorted_dict = dict( sorted(word_dict.items(), key=operator.itemgetter(1))) print('Sorted Dictionary: ') print(sorted_dict)
Output:
Sorted Dictionary: {'is': 2, 'here': 5, 'at': 9, 'this': 11, 'why': 12}
Here, itemgetter(1) returned the value field from the key-value pair. Therefore, dictionary was sorted by value.
Sort the dictionary by value in descending order using itemgetter in python
In addition to the itemgetter object as a key argument like in the previous example, also pass the reverse argument as True to the sorted() function. It will sort the items in the dictionary by value, but in descending order i.e.
import operator word_dict = { 'this': 11, 'at': 9, 'here': 5, 'why': 12, 'is': 2 } # Sort Dictionary by value using itemgetter sorted_dict = dict(sorted( word_dict.items(), key=operator.itemgetter(1), reverse=True)) print('Sorted Dictionary: ') print(sorted_dict)
Output:
{'why': 12, 'this': 11, 'at': 9, 'here': 5, 'is': 2}
Conclusion:
So these were the two ways to sort a dictionary by value.