Python – Sort List in Descending Order

Python – Sort List in Descending Order

To sort a list in descending order in Python, call sort() method on the list object, and specify the named argument reverse=True.

By default sort() method of a list instance sorts the list in ascending order. reverse=True makes the sort() method sort the list in descending order.

You may refer Python List sort() method for complete information on syntax and examples.

Examples

1. Sort list of numbers in descending order in Python

In the following program, we take a list of numbers in my_list, and sort them descending order using list.sort() method.

Python Program

my_list = [11, 88, 22, 44, 66, 33, 55]
my_list.sort(reverse=True)
print(my_list)
Run Code Copy

Output

[88, 66, 55, 44, 33, 22, 11]

The list is sorted in descending order.

2. Sort list of strings in descending order in Python

In the following program, we take a list of strings in my_list, and lexicographically sort them in descending order using list.sort() method.

Python Program

my_list = ['cherry', 'apple', 'mango', 'banana']
my_list.sort(reverse=True)
print(my_list)
Run Code Copy

Output

['mango', 'cherry', 'banana', 'apple']

The list of strings is sorted in descending order.

Summary

In this Python Lists tutorial, we learned how to sort a list in descending order using list.sort() method.

Related Tutorials

Code copied to clipboard successfully 👍