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.
# Python Program to Sort a List in Descending Order Using sort()
# with option reverse=True Method
NumList = [12,45,87,49,56,46]
# Print the List
print("\nElement Before Sorting is: ", NumList)
NumList.sort(reverse=True)
print("\nElement After Sorting List in Descending Order is:\n", NumList)
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.
# Python Program to Sort a List in Descending Order Using sorted()
# with option reverse=True Method
NumList = [12,45,87,49,56,46]
# Print the List
print("\nElement Before Sorting is: ", NumList)
new_NumList=sorted(NumList,reverse=True)
print("\nElement After Sorting List in Descending Order is: \n", NumList)
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.
# Python Program to Sort a List in Descending Order Using sort() and reverse() Method
NumList = [12,45,87,49,56,46]
# Print the List
print("\nElement Before Sorting is: ", NumList)
NumList.sort()
NumList.reverse()
print("\nElement After Sorting List in Descending Order is: \n", NumList)
Element Before Sorting is: [12, 45, 87, 49, 56, 46]
Element After Sorting List in Descending Order is:
[87, 56, 49, 46, 45, 12]
# Python Program to Sort a List in Descending Order Using For Loop
NumList = [12,45,87,49,56,46]
# Print the List
print("\nElement Before Sorting is: ", NumList)
Number=len(NumList)
for i in range (Number):
for j in range(i + 1, Number):
if(NumList[i] < NumList[j]):
temp = NumList[i]
NumList[i] = NumList[j]
NumList[j] = temp
print("\nElement After Sorting List in Descending Order is: \n", NumList)
Element Before Sorting is: [12, 45, 87, 49, 56, 46]
Element After Sorting List in Descending Order is:
[87, 56, 49, 46, 45, 12]
# Python Program to Sort a List in Descending Order Using While Loop
NumList = [12,45,87,49,56,46]
print("\nElement Before Sorting is: ", NumList)
Number=len(NumList)
i = 0
while(i < Number):
j = i + 1
while(j < Number):
if(NumList[i] < NumList[j]):
temp = NumList[i]
NumList[i] = NumList[j]
NumList[j] = temp
j = j + 1
i = i + 1
print("\nElement After Sorting List in Descending Order is: \n", NumList)
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