Python – Check for descending Sorted list


A List is a type of data structure in Python language that can store elements of different data types within these β€œ[]” brackets. The order in which data can be sorted will be either ascending or descending. In a list when the preceding element is greater than the next element then it follows descending order and on the other hand, when the preceding element is less than the next element then it follows ascending order. Utilizing three distinct approaches, the ways are given verifying for descending Sorted list in python.

Approaches

Approach 1 βˆ’ Using iteration method

Approach 2 βˆ’ Using sort()

Approach 3 βˆ’ Using reduce() method

Approach 1: Python Program to check for descending Sorted list using iteration

The function is defined with one argument for checking through the list. To check through the elements of the list we need to look at whether the previous element is greater than the upcoming element. A for loop is used to iterate through the num_list and then when the condition is satisfied it follows descending order otherwise it is not. The num_list is initialized with a list of elements.

Algorithm

  • Step 1 βˆ’ The function is defined with one argument as val.

  • Step 2 βˆ’ The for loop is used to iterate through the range of the elements of the list.

  • Step 3 βˆ’ To find the descending order, the value β€œa” should be less than the element coming next.

  • Step 4 βˆ’ The num_list is initialized with a list of elements in some order.

  • Step 5 βˆ’ Based on the condition, it checks whether the given list is in descending order or not.

Example

#is_descending function is defined with one argument as val
def is_descending(val):
   # iterate through the list to check whether every element in the list is greater than the succeeding element
   for a in range(1, len(val)):
      if val[a] > val[a-1]:
         return False
      return True
#initializing the list with elements to check the order of the elements
num_list = [45, 32, 12,98, 100]
#using if else loop the resultant statement is printed
if is_descending(num_list):
   print("Given list follows descending order.")
else:
   print("Descending order is not followed.")

Output

Descending order is not followed.

Approach 2: Python Program to check for descending Sorted list using the sort() method

The function is defined with one argument for checking through the list. To check through the elements of the list we need to look at whether the previous element is greater than the upcoming element. The sorted() function is used to check if it is ordered in descending order. The num_list is initialized with a list of elements. Then when the condition is satisfied it follows descending order otherwise it is not.

  • Step 1 βˆ’ Creation of function named is_descending().

  • Step 2 βˆ’ To identify the descending order, the sort() is used to check if the current element is less than the element coming next.

  • Step 3 βˆ’ Initializing the list with elements to check the order of the elements

  • Step 4 βˆ’ Based on the condition, it checks whether the given list is in descending order or not.

Example

#is_descending function is defined with one argument as val
def is_descending(val):
   return val == sorted(val, reverse=True)
num_list = [98, 45, 32, 12]
#using if else loop the resultant statement is printed
if is_descending(num_list):
   print("Given list follows descending order.")
else:
   print("Descending order is not followed.")

Output

Given list follows descending order.

Approach 3: Python Program to check for descending Sorted list using the reduce() method

The functools library is imported to use the reduce() function. Then the function is defined with one argument for checking through the list.

Algorithm

  • Step 1 βˆ’ The functools is imported to use the functions like reduce().

  • Step 2 βˆ’ Creation of a function is_descending() containing one argument as val.

  • Step 3 βˆ’ The reduce() is used to check if the current element is less than the element coming next.

  • Step 4 βˆ’ Create the list named num_list consisting of a list of integer elements in some order.

  • Step 5 βˆ’ Based on the condition, it checks whether the given list is in descending order or not.

Example

#the reduce function is imported
from functools import reduce

#is_descending function is defined with one argument as val
def is_descending(val):
   return reduce(lambda a, b: a > b and a or False, val) != False
    
#initializing the list with elements to check the order of the elements
num_list = [98, 45, 32, 12]
#using the if else loop the resultant statement is printed
if is_descending(num_list):
   print("Given list follows descending order.")
else:
   print("Descending order is not followed.")

Output

Given list follows descending order.

Conclusion

In this article, we use three different approaches, to describe the way checking of descending Sorted lists in Python language. For these, different methods are depicted in all three distinct approaches. In the third approach use the reduce() method, to check through the elements of the list we need to look at whether the previous element is greater than the upcoming element.

Updated on: 25-Aug-2023

575 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements