Verifying that a list is sorted is a common validation step in algorithms, data processing, and unit testing. While you could write a loop to check every element manually, Python provides concise, functional ways to achieve this efficiently.
This guide explores the two most common methods: comparing against a sorted copy (easiest to read) and iterating through pairs (most performant).
Method 1: Comparing with sorted() (Easiest)
The most readable way to check if a list is sorted is to sort a copy of the list and verify if it matches the original.
The sorted() function returns a new list, leaving the original list unchanged. If the original list is identical
to the new sorted list, then the original was already sorted.
numbers = [1, 2, 3, 5, 8]
unsorted_numbers = [3, 1, 4, 1, 5]
is_sorted = numbers == sorted(numbers)
is_unsorted = unsorted_numbers == sorted(unsorted_numbers)
print(f"Is 'numbers' sorted? {is_sorted}")
print(f"Is 'unsorted_numbers' sorted? {is_unsorted}")Output:
Is 'numbers' sorted? True
Is 'unsorted_numbers' sorted? FalseHow to Check for Descending Order
To check if a list is sorted in descending (reverse) order, use the reverse=True parameter.
desc_list = [9, 8, 5, 2, 1]
is_descending = desc_list == sorted(desc_list, reverse=True)
print(f"Is descending? {is_descending}")Output:
Is descending? TrueMethod 2: Using all() and zip() (Efficient)
While Method 1 is readable, it is computationally expensive (O(N log N)) because it sorts the entire list. A more
efficient approach (O(N)) is to iterate through the list and ensure that every element is less than or equal to the
next element.
We can use zip() to pair elements with their neighbors and all() to verify the condition.
numbers = [1, 2, 4, 3, 5]
pairs = zip(numbers, numbers[1:])
is_sorted = all(a <= b for a, b in pairs)
print(f"Is sorted? {is_sorted}")Output:
Is sorted? FalseMethod 3: Using Range Indices
If you prefer avoiding slicing (which creates a shallow copy of the list in memory), you can use indices with a generator expression.
numbers = [1, 2, 3, 4, 5]
is_sorted = all(numbers[i] <= numbers[i+1] for i in range(len(numbers) - 1))
print(f"Is sorted? {is_sorted}")Output:
Is sorted? TruePerformance Comparison
| Method | Time Complexity | Space Complexity | Use Case |
|---|---|---|---|
lst == sorted(lst) |
O(N log N) |
O(N) |
Best for small lists or code readability. |
all(... zip(...)) |
O(N) |
O(N) |
Best for large lists; faster due to short-circuiting. |
all(... range(...)) |
O(N) |
O(1) |
Most memory efficient (no slicing copies). |
Conclusion
To check if a list is sorted:
- Use
lst == sorted(lst)for cleaner code on small lists. - Use
all(a <= b for a, b in zip(lst, lst[1:]))for better performance on large lists. - Remember
reverse=Trueif checking for descending order.