Python Program to Sort a List in Descending Order

Introduction

Sorting a list in descending order is a common task in programming, especially when you need to organize data from highest to lowest. This tutorial will guide you through creating a Python program that sorts a given list of numbers in descending order.

Example:

  • Input: [3, 5, 7, 2, 8]

  • Output: [8, 7, 5, 3, 2]

  • Input: [15, 22, 9, 3, 6, 27]

  • Output: [27, 22, 15, 9, 6, 3]

Problem Statement

Create a Python program that:

  • Takes a list of numbers as input.
  • Sorts the list in descending order.
  • Displays the sorted list.

Solution Steps

  1. Take Input from the User: Use the input() function to get a list of numbers from the user.
  2. Convert Input to a List of Integers: Convert the input string to a list of integers.
  3. Sort the List in Descending Order: Use the sort() method or the sorted() function with the reverse=True argument to sort the list in descending order.
  4. Display the Sorted List: Use the print() function to display the sorted list.

Python Program

# Python Program to Sort a List in Descending Order # Author: https://www.rameshfadatare.com/ # Step 1: Take input from the user input_list = input("Enter a list of numbers separated by spaces: ") # Step 2: Convert the input string to a list of integers numbers = list(map(int, input_list.split())) # Step 3: Sort the list in descending order numbers.sort(reverse=True) # Or you can use: numbers = sorted(numbers, reverse=True) # Step 4: Display the sorted list print(f"The sorted list in descending order is: {numbers}") 

Explanation

Step 1: Take Input from the User

  • The input() function prompts the user to enter a list of numbers, separated by spaces. The input is stored as a string in the variable input_list.

Step 2: Convert Input to a List of Integers

  • The split() method is used to split the input string into individual components, and map(int, ...) is used to convert these components into integers. The list() function then creates a list of these integers.

Step 3: Sort the List in Descending Order

  • The sort() method is used with the reverse=True argument to sort the list in place in descending order. Alternatively, the sorted() function can be used with the reverse=True argument to return a new sorted list in descending order.

Step 4: Display the Sorted List

  • The print() function is used to display the sorted list.

Output Example

Example 1:

Enter a list of numbers separated by spaces: 3 5 7 2 8 The sorted list in descending order is: [8, 7, 5, 3, 2] 

Example 2:

Enter a list of numbers separated by spaces: 15 22 9 3 6 27 The sorted list in descending order is: [27, 22, 15, 9, 6, 3] 

Example 3:

Enter a list of numbers separated by spaces: 1 100 50 23 The sorted list in descending order is: [100, 50, 23, 1] 

Conclusion

This Python program demonstrates how to sort a list of numbers in descending order using the sort() method or the sorted() function with the reverse=True argument. This is a fundamental operation in Python programming, and understanding it is essential for working with lists and data structures effectively.

Leave a Comment

Scroll to Top