How to Sort a List in Descending Order in Python

AuthorSumit Dey Sarkar

Pubish Date22 Apr 2023

categoryPython

In this tutorial we will learn how to sort a list in descending order in Python .

 

Sort a List in Descending Order in Python

Python's sort() method or sorted() function with the reverse=True option can be used to sort a list in descending order.

 

Let's see an example:

my_list = [5, 3, 1, 4, 2]

# Using sort() method
my_list.sort(reverse=True)
print(my_list)  # Output: [5, 4, 3, 2, 1]

# Using sorted() function
new_list = sorted(my_list, reverse=True)
print(new_list)  # Output: [5, 4, 3, 2, 1]

 

In all instances, the list is sorted in descending order using the reverse=True parameter. Always keep that in mind ,the sorted() function returns a new sorted list, whereas the sort() method sorts the list in-place.

Comments 0

Leave a comment