- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to sort the elements of an array in descending order
When it is required to sort the elements of an array in descending order, the ‘sort’ method can be used by specifying a parameter named ‘reverse’ to True.
Below is a demonstration of the same −
my_list = [44, 56, 42, 31, 11, 23, 78, 89, 9, 0] print("The list is :") print(my_list) my_list.sort(reverse = True) print("The list after sorting is :") print(my_list)
The list is : [44, 56, 42, 31, 11, 23, 78, 89, 9, 0] The list after sorting is : [89, 78, 56, 44, 42, 31, 23, 11, 9, 0]
A list is defined, and is displayed on the console.
The ‘sort’ method is called on the list.
A parameter named ‘reverse’ is set to ‘True’ here.
This helps display the elements in descending order.
The output is displayed on the console.
Kickstart Your Career
Get certified by completing the courseGet Started