How to sort in descending order in Python?
To sort a list in Python in descending order, you can use the following method:
- Sort the list in reverse order using the sorted() function with the reverse parameter.
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
sorted_list = sorted(my_list, reverse=True)
print(sorted_list)
- Utilize the sort() method with the reverse parameter.
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
my_list.sort(reverse=True)
print(my_list)
Both methods will sort the list in descending order, and either return a new sorted list or directly sort the original list.