Last updated: Apr 12, 2024
Reading timeยท5 min
To check if a list is sorted in ascending order in Python:
range()
class to iterate over the indices of the list.all()
function.def is_sorted_ascending(lst): return all( lst[index] <= lst[index+1] for index in range(len(lst) - 1) ) list_1 = [1, 2, 3, 4, 5] print(is_sorted_ascending(list_1)) # ๐๏ธ True list_2 = [5, 4, 3, 2, 1] print(is_sorted_ascending(list_2)) # ๐๏ธ False if is_sorted_ascending(list_1): print('The list is sorted in ascending order') else: print('The list is not sorted in ascending order')
The is_sorted_ascending()
function takes a list as a parameter and returns
True
if the list is sorted in ascending order.
If the list is not sorted in ascending order, the function returns False
.
We used the range class to iterate over the indices in the supplied list.
def is_sorted_ascending(lst): return all( lst[index] <= lst[index+1] for index in range(len(lst) - 1) )
The range() class is commonly used for looping a specific number of times in for loops.
Notice that we subtract 1
from the result of calling the
len() function with the list.
This is because we don't want to check if the last item is less than or equal to an item after that (that doesn't exist).
list_1 = [1, 2, 3, 4, 5] # [0, 1, 2, 3] print(list(range(len(list_1) - 1)))
We used a generator expression to iterate over the range
object.
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.
On each iteration, we check if the list item at the current index is less than or equal to the list item at the next index.
def is_sorted_ascending(lst): return all( lst[index] <= lst[index+1] for index in range(len(lst) - 1) )
If the condition is met for all list elements, then the list is sorted in ascending order.
The all() built-in function
takes an iterable as an argument and returns True
if all elements in the
iterable are truthy (or the iterable is empty).
To check if a list is sorted in descending order in Python:
range()
class to iterate over the indices of the list.all()
function.def is_sorted_descending(lst): return all( lst[index] >= lst[index+1] for index in range(len(lst) - 1) ) list_1 = [5, 4, 3, 2, 1] print(is_sorted_descending(list_1)) # ๐๏ธ True list_2 = [1, 2, 3, 4, 5] print(is_sorted_descending(list_2)) # ๐๏ธ False if is_sorted_descending(list_1): print('The list is sorted in DESCENDING order') else: print('The list is not sorted in DESCENDING order')
We used the range()
class to get a range object that contains the indices of
the list up to (but not including the last index).
The function takes a list and returns True
if the list is sorted in descending
order.
Otherwise, False
is returned.
def is_sorted_descending(lst): return all( lst[index] >= lst[index+1] for index in range(len(lst) - 1) )
We used a generator expression to iterate over the indices in the range
object.
On each iteration, we check if the current list item is greater than or equal to the next list item.
If the condition is met for all list elements, then the list is sorted in descending order.
The all() built-in function
takes an iterable as an argument and returns True
if all elements in the
iterable are truthy (or the iterable is empty).
sorted()
functionYou can also use the sorted()
function to check if a list is sorted in Python.
The sorted function returns a new sorted list from the items in the iterable.
def is_sorted_ascending(lst): return sorted(lst) == lst list_1 = [1, 2, 3, 4, 5] print(is_sorted_ascending(list_1)) # ๐๏ธ True list_2 = [5, 4, 3, 2, 1] print(is_sorted_ascending(list_2)) # ๐๏ธ False if is_sorted_ascending(list_1): print('The list is sorted in Ascending order') else: print('The list is not sorted in Ascending order')
The function sorts the list by using the sorted()
function and compares the
sorted list to the supplied list.
If the two lists are equal, then the supplied list is sorted.
itertools.pairwise()
You can also use the itertools.pairwise() class to check if a list is sorted.
from itertools import pairwise def is_sorted_ascending(lst): return all(x <= y for x, y in pairwise(lst)) list_1 = [1, 2, 3, 4, 5] print(is_sorted_ascending(list_1)) # ๐๏ธ True list_2 = [5, 4, 3, 2, 1] print(is_sorted_ascending(list_2)) # ๐๏ธ False if is_sorted_ascending(list_1): print('The list is sorted in Ascending order') else: print('The list is not sorted in Ascending order')
The itertools.pairwise()
method returns successive overlapping pairs taken
from the supplied iterable.
from itertools import pairwise list_1 = [1, 2, 3, 4, 5] # [(1, 2), (2, 3), (3, 4), (4, 5)] print(list(pairwise(list_1)))
On each iteration, we check if the first item in the current tuple is less than or equal to the second item.
from itertools import pairwise def is_sorted_ascending(lst): return all(x <= y for x, y in pairwise(lst))
If the condition is met on all iterations, then the list is sorted in ascending order.
itertools.pairwise()
The following code sample checks if a list is sorted in descending order using
itertools.pairwise()
.
from itertools import pairwise def is_sorted_descending(lst): return all(x >= y for x, y in pairwise(lst)) list_1 = [5, 4, 3, 2, 1] print(is_sorted_descending(list_1)) # ๐๏ธ True list_2 = [1, 2, 3, 4, 5] print(is_sorted_descending(list_2)) # ๐๏ธ False if is_sorted_descending(list_1): print('The list is sorted in DESCENDING order') else: print('The list is not sorted in DESCENDING order')
The itertools.pairwise()
method returns successive overlapping pairs taken
from the supplied iterable.
from itertools import pairwise list_1 = [1, 2, 3, 4, 5] # [(1, 2), (2, 3), (3, 4), (4, 5)] print(list(pairwise(list_1)))
On each iteration, we check if the first tuple item is greater than or equal to the next tuple item.
from itertools import pairwise def is_sorted_descending(lst): return all(x >= y for x, y in pairwise(lst))
If the condition is met on all iterations, then the list is sorted in descending order.
I've also written an article on how to get the Indices of a sorted List in Python.
You can learn more about the related topics by checking out the following tutorials: