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 −

Example

 Live Demo

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.

Updated on: 16-Apr-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements