Python Program to Sort List Items in Descending Order

Write a Python program to sort list items in descending order. In this example, we used the sort and reverse methods to sort the list in descending order.

intlist = []
intlistTot = int(input("Total Number of List Items to Sort = "))

for i in range(1, intlistTot + 1):
    intlistvalue = int(input("Please enter the %d List Item = "  %i))
    intlist.append(intlistvalue)

intlist.sort()
intlist.reverse()

print('List Items After Sorting in Descending Order')
print(intlist)
Program to Sort List Items in Descending Order

In this Python program, we used the nested for loops and temp variables to sort the Python list items in descending order.

intlist = []
intlistTot = int(input("Total Number of List Items to Sort = "))

for i in range(1, intlistTot + 1):
    intlistvalue = int(input("Please enter the %d List Item = "  %i))
    intlist.append(intlistvalue)

for i in range(len(intlist)):
    for j in range(i + 1, len(intlist)):
        if(intlist[i] < intlist[j]):
            temp = intlist[i]
            intlist[i] = intlist[j]
            intlist[j] = temp
    
print('List Items After Sorting in Descending Order')
for i in range(len(intlist)):
    print(intlist[i], end = '   ')
Total Number of List Items to Sort = 8
Please enter the 1 List Item = 17
Please enter the 2 List Item = 47
Please enter the 3 List Item = 12
Please enter the 4 List Item = 99
Please enter the 5 List Item = 55
Please enter the 6 List Item = 4
Please enter the 7 List Item = 60
Please enter the 8 List Item = 75
List Items After Sorting in Descending Order
99   75   60   55   47   17   12   4   

This program helps to sort list items in descending order using a while loop.

intlist = []
intlistTot = int(input("Total Number of List Items to Sort = "))

i = 1
while(i <= intlistTot):
    intlistvalue = int(input("Please enter the %d List Item = "  %i))
    intlist.append(intlistvalue)
    i = i + 1

i = 0
while(i < len(intlist)):
    j = i + 1
    while j < len(intlist):
        if(intlist[i] < intlist[j]):
            temp = intlist[i]
            intlist[i] = intlist[j]
            intlist[j] = temp
        j = j + 1
    i = i + 1
    
print('Descending Order')
i = 0
while(i < len(intlist)):
    print(intlist[i], end = '   ')
    i = i + 1
Total Number of List Items to Sort = 7
Please enter the 1 List Item = 19
Please enter the 2 List Item = 55
Please enter the 3 List Item = 89
Please enter the 4 List Item = 32
Please enter the 5 List Item = 47
Please enter the 6 List Item = 15
Please enter the 7 List Item = 7
Descending Order
89   55   47   32   19   15   7   

By default, the built-in sort() function sorts a list of strings in alphabetical order. So, if we use the sort() function with the reverse argument, it sorts strings from Z to A. Refer to Python sort.

holidays = ['kerala', 'London', 'Amsterdam', 'Rome']

holidays.sort(reverse=True)
print(holidays)
['kerala', 'Rome', 'London', 'Amsterdam']

If the task is to sort the string items in descending order based on their length (word count), add the key argument with the len() function. Refer to Python len.

holidays = ['kerala', 'London', 'Amsterdam', 'Rome']

holidays.sort(reverse=True, key= len)
print(holidays)
['Amsterdam', 'kerala', 'London', 'Rome']

Also Read

  1. Python program to Print Elements in a List
  2. Python Program to Print List Items in Reverse Order