Sorting List of Dictionaries in Descending Order in Python
Last Updated :
06 Feb, 2024
Sorting is one of the most common things in Python and is used in day-to-day programming. In this article, we will sort the list of dictionaries in descending order in Python.
Sorting The List of Dictionaries In Descending Order
There are different methods to Sort The List Of Dictionaries In Descending Order using Python. Below we are explaining all the possible approaches with proper practical implementation.
- Using the sorted Function with a Lambda Function
- Using the
sorted
Function with Itemgetter
- Using the
sort()
method with a Custom Comparator
- Using List Comprehension with Sorted
- Using a Custom Function
Sorting The List Of Dictionaries In Descending Order Using sorted
() Method
In this method, we use sorted() function with a lambda function as the key argument to sort the list of dictionaries based on the ‘age’ key in descending order.
Python3
list_of_dicts = [{ 'name' : 'Alice' , 'age' : 30 }, { 'name' : 'Bob' , 'age' : 25 }, { 'name' : 'Charlie' , 'age' : 35 }]
sorted_list = sorted (list_of_dicts, key = lambda x: x[ 'age' ], reverse = True )
print (sorted_list)
|
Output
[{'name': 'Charlie', 'age': 35}, {'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}]
Sorting List Of Dictionaries In Descending Order Using Itemgetter
In this example, we use itemgetter
function from the operator
module as the key argument in the sorted()
function to sort the list of dictionaries based on the ‘age’ key in descending order.
Python3
from operator import itemgetter
list_of_dicts = [{ 'class' : '5' , 'section' : 3 }, { 'Class' : 'Five' , 'section' : 7 }, { 'Class' : 'Five' , 'section' : 2 }]
sorted_list = sorted (list_of_dicts, key = itemgetter( 'section' ), reverse = True )
print (sorted_list)
|
Output
[{'Class': 'Five', 'section': 7}, {'class': '5', 'section': 3}, {'Class': 'Five', 'section': 2}]
Sorting List Of Dictionaries In Descending Order Using the sort()
Method
In this method, we use sort()
method is with a lambda function as the key to sort the list of dictionaries in-place based on the ‘age’ key in descending order.
Python3
list_of_dicts = [{ 'name' : 'tarjan' , 'height' : 30 }, { 'name' : 'blon' , 'height' : 89 }, { 'name' : 'starc' , 'height' : 75 }]
list_of_dicts.sort(key = lambda x: x[ 'height' ], reverse = True )
print (list_of_dicts)
|
Output
[{'name': 'blon', 'height': 89}, {'name': 'starc', 'height': 75}, {'name': 'tarjan', 'height': 30}]
Sorting List Of Dictionaries In Descending Order Using List Comprehension
In this method, List comprehension is used along with the sorted() function to create a new sorted list of dictionaries based on the ‘age’ key in descending order.
Python3
list_of_dicts = [{ 'word' : 'Geeks' , 'letter' : 1 }, { 'word' : 'Geeks' , 'letter' : 3 }, { 'word' : 'for' , 'letter' : 2 }]
sorted_list = [x for x in sorted (list_of_dicts, key = lambda x: x[ 'letter' ], reverse = True )]
print (sorted_list)
|
Output
[{'word': 'Geeks', 'letter': 3}, {'word': 'for', 'letter': 2}, {'word': 'Geeks', 'letter': 1}]
Sorting The List Of Dictionaries In Descending Order Using a Custom Function
In this example, a custom sorting function is defined and is used as the key argument in the sorted()
function to sort the list of dictionaries based on the ‘age’ key in descending order.
Python3
def custom_sort(dictionary):
return dictionary[ 'age' ]
list_of_dicts = [{ 'name' : 'Alice' , 'age' : 30 }, { 'name' : 'Bob' , 'age' : 25 }, { 'name' : 'Charlie' , 'age' : 35 }]
sorted_list = sorted (list_of_dicts, key = custom_sort, reverse = True )
print (sorted_list)
|
Output
[{'name': 'Charlie', 'age': 35}, {'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}]
Share your thoughts in the comments
Please Login to comment...