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

Last updated: Apr 12, 2024
5 min

## #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
```Copied!```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.

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

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
```Copied!```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
```Copied!```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.

## #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
```Copied!```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.

main.py
```Copied!```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
```Copied!```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
```Copied!```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.

main.py
```Copied!```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
```Copied!```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.