In this python program, we will learn how to sort a list in descending order. There are various ways to sort a list in descending order in python. We can sort the list using python built-in functions such as using sort()
, sorted()
, sort()
and reverse()
or using loops.
Here is the source code of the program to sort a list in descending order.
In this program, we used the python built-in sort()
function with option reverse=True
. The sort()
function sorts the list and reverse=True
reverse the sorted list in descending order.
Element Before Sorting is: [12, 45, 87, 49, 56, 46]
Element After Sorting List in Descending Order is:
[87, 56, 49, 46, 45, 12]
In this program, we used the sorted()
function to sort the list using reverse=True
option. The sorted()
function sort the list and reverse=True
reverse the list in descending order.
Element Before Sorting is: [12, 45, 87, 49, 56, 46]
Element After Sorting List in Descending Order is:
[12, 45, 87, 49, 56, 46]
In this program, we used the python built-in function sort()
and reverse()
. The sort()
function sorts the list in ascending order and the reverse()
function reverses the list in descending order.
Element Before Sorting is: [12, 45, 87, 49, 56, 46]
Element After Sorting List in Descending Order is:
[87, 56, 49, 46, 45, 12]
Element Before Sorting is: [12, 45, 87, 49, 56, 46]
Element After Sorting List in Descending Order is:
[87, 56, 49, 46, 45, 12]
Element Before Sorting is: [12, 45, 87, 49, 56, 46]
Element After Sorting List in Descending Order is:
[87, 56, 49, 46, 45, 12]
Comments