Editor

Python Program to Sort a List in Descending Order

Beginner · Searching and sorting

Descending order runs from the largest value to the smallest. This program sorts numbers and names in descending order with reverse=True, and shows the difference between sorted(), which makes a new list, and sort(), which changes the list itself.

Program

sort_descending.py
numbers = [42, 7, 19, 88, 3, 56] # or: numbers = [int(x) for x in input("Enter numbers: ").split()]
# sorted() returns a new list and leaves the original alone
descending = sorted(numbers, reverse=True)
print("Original list:", numbers)
print("Descending order:", descending)
# sort() changes the list itself
numbers.sort(reverse=True)
print("After numbers.sort(reverse=True):", numbers)
# Names: capital letters sort before small ones
names = ["Meera", "arjun", "Zoya", "Kabir"]
print("Names, Z to A:", sorted(names, reverse=True))
print("Ignoring case:", sorted(names, key=str.lower, reverse=True))
Output
Original list: [42, 7, 19, 88, 3, 56]
Descending order: [88, 56, 42, 19, 7, 3]
After numbers.sort(reverse=True): [88, 56, 42, 19, 7, 3]
Names, Z to A: ['arjun', 'Zoya', 'Meera', 'Kabir']
Ignoring case: ['Zoya', 'Meera', 'Kabir', 'arjun']

How it works

  1. reverse=True tells sorted() and sort() to put the largest value first.
  2. sorted(numbers, ...) returns a new list, so numbers is still in its original order when it is printed.
  3. numbers.sort(...) sorts the list in place and returns None, so do not write numbers = numbers.sort().
  4. Strings compare by character codes, and every capital letter comes before every small letter, so "arjun" is placed first in Z to A order.
  5. key=str.lower compares the names as if they were all small letters, which gives the order people expect.

Other ways to write it

Without sort functions

Without sort functions
numbers = [42, 7, 19, 88, 3, 56]
for i in range(len(numbers)):
for j in range(i + 1, len(numbers)):
if numbers[j] > numbers[i]: # a bigger number moves to the front
numbers[i], numbers[j] = numbers[j], numbers[i]
print("Descending order:", numbers)
Output
Descending order: [88, 56, 42, 19, 7, 3]

Each position i is compared with every later position, and any bigger number is swapped forward, so the largest values collect at the front.

Sort ascending, then reverse

Sort ascending, then reverse
numbers = [42, 7, 19, 88, 3, 56]
numbers.sort()
numbers.reverse()
print("Descending order:", numbers)
print("Using slicing:", sorted([42, 7, 19, 88, 3, 56])[::-1])
Output
Descending order: [88, 56, 42, 19, 7, 3]
Using slicing: [88, 56, 42, 19, 7, 3]

Sorting from smallest to largest and then reversing gives the same result in two steps.

Try it yourself

Sort the list [3.5, -2, 10, 0, 7.25] in descending order and check where the negative number ends up.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises