Python sort reverse order | Example code

Use sort() with reverse=True to sort a list in reverse order in Python. The sort() method sorts the list ascending by default.

Python sort reverse example

Simple example code use reverse attribute in the sort method. Set it equal to True to sort the list in reverse order in place.

a_list = [1, 2, 3]

a_list.sort(reverse=True)

print(a_list)

Output:

Python sort reverse order

Do comment if you have any doubts and suggestions on this Python sort tutorial.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

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