0
0
PythonHow-ToBeginner · 3 min read

How to Sort List in Descending Order in Python

To sort a list in descending order in Python, use the sorted() function or the list's sort() method with the argument reverse=True. For example, sorted(my_list, reverse=True) returns a new sorted list, while my_list.sort(reverse=True) sorts the list in place.
📐

Syntax

There are two main ways to sort a list in descending order in Python:

  • Using sorted() function: Returns a new sorted list without changing the original.
  • Using list.sort() method: Sorts the list in place, changing the original list.

Both accept the reverse=True argument to sort in descending order.

python
sorted_list = sorted(my_list, reverse=True)
my_list.sort(reverse=True)
💻

Example

This example shows how to sort a list of numbers in descending order using both sorted() and list.sort().

python
my_list = [5, 2, 9, 1, 7]

# Using sorted() returns a new list
sorted_list = sorted(my_list, reverse=True)
print("Sorted with sorted():", sorted_list)

# Using sort() changes the original list
my_list.sort(reverse=True)
print("Sorted with sort():", my_list)
Output
Sorted with sorted(): [9, 7, 5, 2, 1] Sorted with sort(): [9, 7, 5, 2, 1]
⚠️

Common Pitfalls

Common mistakes when sorting in descending order include:

  • Forgetting to use reverse=True, which sorts in ascending order by default.
  • Using sorted() but expecting the original list to change (it does not).
  • Using list.sort() but expecting a new list returned (it returns None).
python
my_list = [3, 8, 4]

# Wrong: forget reverse=True (sorts ascending)
print(sorted(my_list))  # Output: [3, 4, 8]

# Wrong: expecting sort() to return new list
result = my_list.sort(reverse=True)
print(result)  # Output: None
print(my_list)  # List is sorted in place
Output
[3, 4, 8] None [8, 4, 3]
📊

Quick Reference

MethodDescriptionModifies Original ListReturns
sorted(my_list, reverse=True)Returns new list sorted descendingNoNew sorted list
my_list.sort(reverse=True)Sorts original list descendingYesNone

Key Takeaways

Use reverse=True to sort lists in descending order in Python.
sorted() returns a new sorted list without changing the original.
list.sort() sorts the list in place and returns None.
Forgetting reverse=True sorts the list in ascending order by default.
Choose sorted() when you need a new list, and sort() to modify the original.