Sort List in Descending Order in Python

Python provides built-in functions to sort lists in ascending or descending order. The sort() function is used to sort a list in ascending order, while the sorted() function is used to return a new sorted list in ascending order without modifying the original list. To sort a list in descending order, we can use the reverse=True parameter with either of these functions.

Python list sort in descending order

To sort the python list in descending order you can use the Python sort() function along with the reverse=True parameter. This will return a list in descending order.

See the example below:

numbers = [4, 2, 7, 1, 3]
numbers.sort(reverse=True)
print(numbers) 
# Output: [7, 4, 3, 2, 1]

numbers = [4, 2, 7, 1, 3]
sorted_numbers = sorted(numbers, reverse=True)
print(sorted_numbers) 
# Output: [7, 4, 3, 2, 1]

Sort List of String in Descending Order

In python, we can also sort lists of strings in descending order. See the below example:

fruits = ["apple", "banana", "cherry", "date", "elderberry"]
fruits.sort(reverse=True)
print(fruits) 
# Output: ['elderberry', 'date', 'cherry', 'banana', 'apple']

fruits = ["apple", "banana", "cherry", "date", "elderberry"]
sorted_fruits = sorted(fruits, reverse=True)
print(sorted_fruits) 
# Output: ['elderberry', 'date', 'cherry', 'banana', 'apple']

Difference between sort() and sorted() functions in Python

When it comes to sorting a list in Python, there are two main functions you can use: sort() and sorted(). While both functions sort the list, they differ in how they do it.

The sort() function

The sort() function is a method that is called on a list and sorts the elements in the list in ascending order. This function modifies the original list in place, meaning that the original list is changed after calling the function.

fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']

fruits.sort()

print(fruits) 
# Output: ['apple', 'banana', 'cherry', 'date', 'elderberry']

The sorted() function

The sorted() function, on the other hand, is a built-in Python function that takes a list as an argument and returns a new sorted list without modifying the original list. This function can also take an optional parameter reverse to sort the list in descending order.

fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']

sorted_fruits = sorted(fruits)

print(sorted_fruits) 
# Output: ['apple', 'banana', 'cherry', 'date', 'elderberry']
print(fruits) 
# Output: ['apple', 'banana', 'cherry', 'date', 'elderberry']

Using the list() function with the reversed()

numbers = [3, 6, 1, 8, 2]
numbers_reversed = list(reversed(numbers))
numbers_reversed.sort()
sorted_numbers = list(reversed(numbers_reversed))
print(sorted_numbers) 
# Output: [8, 6, 3, 2, 1]

Summary and Conclusion

In this article, we have learned how to sort a list in descending in Python. I hope this article was helpful. If you have any questions you can leave them in the comment section.

Leave a Comment

Scroll to Top