![]() |
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 OrderThere 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.
Sorting The List Of Dictionaries In Descending Order Using
|
# Using the sorted() Function with a Lambda Function 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 ) #Display the soted list of dictionaries print (sorted_list) |
[{'name': 'Charlie', 'age': 35}, {'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}]
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.
# Using the sorted() Function with Itemgetter 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 ) #Display the list of dictionaries print (sorted_list) |
[{'Class': 'Five', 'section': 7}, {'class': '5', 'section': 3}, {'Class': 'Five', 'section': 2}]
sort()
MethodIn 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.
# Method 3: Using the sort() Method with a Custom Comparator 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 ) #Display the list of dictionaries print (list_of_dicts) |
[{'name': 'blon', 'height': 89}, {'name': 'starc', 'height': 75}, {'name': 'tarjan', 'height': 30}]
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.
# Using List Comprehension with Sorted 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 )] #Display sorted list print (sorted_list) |
[{'word': 'Geeks', 'letter': 3}, {'word': 'for', 'letter': 2}, {'word': 'Geeks', 'letter': 1}]
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.
# Using a Custom Function 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 ) #Display sorted list print (sorted_list) |
[{'name': 'Charlie', 'age': 35}, {'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}]
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |