Skip to content

Python sort descending | Example code

  • by

Use the reverse key to sort descending order in Python. If True, the sorted list is reversed (or sorted in Descending order).

list.sort(key=..., reverse=...)

Note: reverse parameter as an optional argument.

Example sort descending in Python

Simple example code Sort the List in Descending Order.

list_name.sort(reverse=True) 

Sorting list of Integers in descending

numbers = [1, 3, 4, 2]

numbers.sort(reverse=True)

print(numbers)

Output:

Python sort descending

Or use this code for the sorted() method.

sorted(list, reverse=True)

Both of these approaches will result in the list being sorted in descending order.

Note: the sorted() function returns a new sorted list while leaving the original list unchanged, while the sort() method sorts the list in place and doesn’t return a new list. Choose the approach that best suits your needs based on whether you want to retain the original list or not.

Do comment if you have any doubts or suggestions on this Python sort topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading