Python offers several elegant ways to determine if a list is sorted in descending order. This is a common task in data processing and algorithm design. Let’s explore efficient methods to achieve this, along with clear code examples.
Method 1: Using a Loop
The most straightforward approach involves iterating through the list and comparing adjacent elements. If at any point, a preceding element is smaller than its successor, the list is not sorted in descending order.
def is_sorted_descending_loop(data):
"""Checks if a list is sorted in descending order using a loop.
Args:
data: The input list.
Returns:
True if the list is sorted in descending order, False otherwise.
"""
for i in range(len(data) - 1):
if data[i] < data[i+1]:
return False
return True
= [9, 7, 5, 3, 1]
my_list print(f"Is {my_list} sorted descending (loop)? {is_sorted_descending_loop(my_list)}") # Output: True
= [9, 7, 10, 3, 1]
my_list print(f"Is {my_list} sorted descending (loop)? {is_sorted_descending_loop(my_list)}") # Output: False
= []
my_list print(f"Is {my_list} sorted descending (loop)? {is_sorted_descending_loop(my_list)}") # Output: True (empty list considered sorted)
= [5]
my_list print(f"Is {my_list} sorted descending (loop)? {is_sorted_descending_loop(my_list)}") # Output: True (single-element list considered sorted)
Method 2: Using all()
and zip()
This method leverages Python’s built-in functions for a more concise solution. zip()
pairs consecutive elements, and all()
checks if the condition (preceding element >= succeeding element) holds true for all pairs.
def is_sorted_descending_all(data):
"""Checks if a list is sorted in descending order using all() and zip().
Args:
data: The input list.
Returns:
True if the list is sorted in descending order, False otherwise.
"""
return all(data[i] >= data[i+1] for i in range(len(data)-1))
= [9, 7, 5, 3, 1]
my_list print(f"Is {my_list} sorted descending (all/zip)? {is_sorted_descending_all(my_list)}") # Output: True
= [9, 7, 10, 3, 1]
my_list print(f"Is {my_list} sorted descending (all/zip)? {is_sorted_descending_all(my_list)}") # Output: False
= []
my_list print(f"Is {my_list} sorted descending (all/zip)? {is_sorted_descending_all(my_list)}") # Output: True
= [5]
my_list print(f"Is {my_list} sorted descending (all/zip)? {is_sorted_descending_all(my_list)}") # Output: True
Method 3: Using sorted()
(for comparison)
While not the most efficient for just checking if a list is sorted, using sorted()
provides a simple way to compare the original list with its descending sorted version. Note that this method is less efficient than the previous methods for large lists.
def is_sorted_descending_sorted(data):
"""Checks if a list is sorted in descending order by comparing to a sorted version.
Args:
data: The input list.
Returns:
True if the list is sorted in descending order, False otherwise.
"""
return data == sorted(data, reverse=True)
= [9, 7, 5, 3, 1]
my_list print(f"Is {my_list} sorted descending (sorted)? {is_sorted_descending_sorted(my_list)}") # Output: True
= [9, 7, 10, 3, 1]
my_list print(f"Is {my_list} sorted descending (sorted)? {is_sorted_descending_sorted(my_list)}") # Output: False
= []
my_list print(f"Is {my_list} sorted descending (sorted)? {is_sorted_descending_sorted(my_list)}") # Output: True
= [5]
my_list print(f"Is {my_list} sorted descending (sorted)? {is_sorted_descending_sorted(my_list)}") # Output: True
Each method offers a different approach to solving the problem. The loop method is explicit, while the all()
and zip()
method is more concise. The sorted()
method offers a simpler, albeit less efficient, alternative. Choose the method that best suits your coding style and performance requirements.