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)

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

Note: IDE: PyCharm 2021.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

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.