
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort Elements of an Array in Descending Order using Python
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 −
Example
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)
Output
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]
Explanation
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.
Advertisements