Check if a List is Sorted (ascending/descending) in Python

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
5 min

banner

# Table of Contents

  1. Check if a List is Sorted (Ascending) in Python
  2. Check if a List is Sorted (Descending) in Python
  3. Check if a list is sorted in Python using the sorted() function
  4. Check if a List is Sorted (Ascending) in Python using itertools.pairwise()
  5. Check if a List is Sorted (Descending) in Python using itertools.pairwise()

# Check if a List is Sorted (Ascending) in Python

To check if a list is sorted in ascending order in Python:

  1. Use the range() class to iterate over the indices of the list.
  2. Check if each item is less than or equal to the item at the next index.
  3. Pass the result to the all() function.
main.py
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')

check if list is sorted in ascending order

The code for this article is available on GitHub

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.

main.py
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).

main.py
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.

main.py
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).

# Check if a List is Sorted (Descending) in Python

To check if a list is sorted in descending order in Python:

  1. Use the range() class to iterate over the indices of the list.
  2. Check if each item is greater than or equal to the item at the next index.
  3. Pass the result to the all() function.
main.py
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')

check if list is sorted in descending order in python

The code for this article is available on GitHub

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).

We exclude the last index because we don't want to check if the last list item is greater than or equal to the next item (that doesn't exist).

The function takes a list and returns True if the list is sorted in descending order.

Otherwise, False is returned.

main.py
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).

# Check if a list is sorted in Python using the sorted() function

You 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.

main.py
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')

check if list is sorted in python using sorted function

The code for this article is available on GitHub

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.

# Check if a List is Sorted (Ascending) in Python using itertools.pairwise()

You can also use the itertools.pairwise() class to check if a list is sorted.

main.py
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')

check if list is sorted using itertools pairwise

The code for this article is available on GitHub

The itertools.pairwise() method returns successive overlapping pairs taken from the supplied iterable.

main.py
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.

main.py
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.

# Check if a List is Sorted (Descending) in Python using itertools.pairwise()

The following code sample checks if a list is sorted in descending order using itertools.pairwise().

main.py
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')

check if list is sorted in descending order using itertools pairwise

The code for this article is available on GitHub

The itertools.pairwise() method returns successive overlapping pairs taken from the supplied iterable.

main.py
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.

main.py
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev