About Python List Sorting
In Python, list maintains the order of the elements as they are inserted or created.
So, list elements can be sorted in descending or ascending order.
Sorting Python list elements in descending order
Using sort(reverse=True) Method:
In Python, you can use the sort() method to sort the elements of a list in descending order.
The sort() method modifies the original list in place and sorts the elements in ascending or descending order based on the specified parameters. The reverse=True parameter is used to sort the elements in descending order.
Let us create a list of string elements and sort them in descending order using the sort() method with reverse=True parameter.
-
Create a list of string with fruit names (
fruit_list = ["Banana", "Apple", "Orange", "Cherry"]) wherefruit_listis the list variable and["Banana", "Apple", "Orange", "Cherry"]is the list with string type elements. The list elements are not in descending order. -
To sort the elements of the above list, you can use
fruit_list.sort(reverse=True)wheresort(reverse=True)method is used to sort the list elements in descending order. If you now print the fruit_list, the list elements will be printed in descending order.
Please note that 'reverse=True' has been passed to the sort() method as argument to sort the list elements in descending order.
In the second example, a list of integer numbers has been sorted in descending order.
Please take a closer look at the following examples:
Example of sorting list elements using sort(reverse=True) method
Output of the above example
Sorted list of string elements: ['Orange', 'Cherry', 'Banana', 'Apple'] Sorted list of integer elements: [5, 4, 3, 2, 1]
Using sorted(list, reverse=True) function:
In Python, you can also use the built-in sorted() function to sort the elements of a list in descending order.
The sorted() function takes a list as an argument and returns a new list with the elements sorted in ascending or descending order based on the specified parameters. The reverse=True parameter is used to sort the elements in descending order.
Let us create a list of string elements and sort them in descending order using the sorted() function with reverse=True parameter.
-
Create a list of string with fruit names
fruit_list = ["Banana", "Apple", "Orange", "Cherry"]wherefruit_listis the list variable and["Banana", "Apple", "Orange", "Cherry"]is the list with string type elements. The list elements are not in descending order. -
Use
sorted_list = sorted(fruit_list, reverse=True)wheresorted_listis a variable andsorted(fruit_list, reverse=True)is the sorted() function with list argument. If you now print the sorted_list, the list elements will be printed in descending order.
Please note that 'fruit_list' and 'reverse=True' have been passed to the sorted() function as arguments to sort the list elements in descending order.
In the second example, a list of integer numbers has been sorted in descending order.
Please take a closer look at the following examples:
Example of sorting list elements using sorted(list, reverse=True) function
Output of the above example
Sorted list of string elements: ['Orange', 'Cherry', 'Banana', 'Apple'] Sorted list of integer elements: [5, 4, 3, 2, 1]