0
0
PythonProgramBeginner · 2 min read

Python Program to Check if List is Sorted

You can check if a list is sorted in Python by using all(lst[i] <= lst[i+1] for i in range(len(lst)-1)), which returns True if the list is sorted in ascending order and False otherwise.
📋

Examples

Input[1, 2, 3, 4, 5]
OutputTrue
Input[5, 3, 4, 1, 2]
OutputFalse
Input[10]
OutputTrue
🧠

How to Think About It

To check if a list is sorted, compare each item with the next one. If every item is less than or equal to the next, the list is sorted. If any item is greater than the next, the list is not sorted.
📐

Algorithm

1
Get the list input.
2
Compare each element with the next element in the list.
3
If all elements are less than or equal to the next, the list is sorted.
4
If any element is greater than the next, the list is not sorted.
5
Return True if sorted, otherwise False.
💻

Code

python
def is_sorted(lst):
    return all(lst[i] <= lst[i+1] for i in range(len(lst) - 1))

# Example usage
print(is_sorted([1, 2, 3, 4, 5]))  # True
print(is_sorted([5, 3, 4, 1, 2]))  # False
print(is_sorted([10]))             # True
Output
True False True
🔍

Dry Run

Let's trace the list [1, 2, 3, 4, 5] through the code

1

Start checking pairs

Compare 1 <= 2: True

2

Next pair

Compare 2 <= 3: True

3

Next pair

Compare 3 <= 4: True

4

Next pair

Compare 4 <= 5: True

5

All pairs checked

All comparisons are True, so return True

PairComparison Result
1 <= 2True
2 <= 3True
3 <= 4True
4 <= 5True
💡

Why This Works

Step 1: Compare each pair

The code checks every pair of neighbors in the list using lst[i] <= lst[i+1] to ensure order.

Step 2: Use all() function

The all() function returns True only if every comparison is True, meaning the list is sorted.

Step 3: Return result

If any pair is out of order, all() returns False, so the function returns False indicating the list is not sorted.

🔄

Alternative Approaches

Using sorted() comparison
python
def is_sorted(lst):
    return lst == sorted(lst)

print(is_sorted([1, 2, 3]))  # True
print(is_sorted([3, 2, 1]))  # False
This method is simple but slower for large lists because it creates a sorted copy.
Using a loop with early exit
python
def is_sorted(lst):
    for i in range(len(lst) - 1):
        if lst[i] > lst[i+1]:
            return False
    return True

print(is_sorted([1, 2, 3]))  # True
print(is_sorted([3, 2, 1]))  # False
This method stops checking as soon as it finds the list is not sorted, which can be faster.

Complexity: O(n) time, O(1) space

Time Complexity

The code checks each pair once, so it runs in linear time relative to the list size.

Space Complexity

The check uses constant extra space, only storing loop variables and no extra lists.

Which Approach is Fastest?

The all() method and loop with early exit are fastest because they stop early if unsorted; sorting the list is slower due to extra work.

ApproachTimeSpaceBest For
all() with generatorO(n)O(1)Clean and efficient for most cases
sorted() comparisonO(n log n)O(n)Simple but slower for large lists
Loop with early exitO(n) best caseO(1)Fast exit on unsorted lists
💡
Use all() with a generator expression for a clean and efficient sorted check.
⚠️
Beginners often forget to handle lists with one or zero elements, which are always sorted.