Check if List is Sorted in Python

Check whether a list is sorted in non-decreasing order.

BeginnerList ProgramsExample 11 of 25
check-if-list-is-sorted.py
1# Program to check if a list is sorted
2
3numbers = list(map(float, input("Enter numbers separated by space: ").split()))
4
5if numbers == sorted(numbers):
6 print("List is sorted in non-decreasing order.")
7else:
8 print("List is not sorted.")

Output

Enter numbers separated by space: 1 2 3 4
List is sorted in non-decreasing order.

What's going on

We compare the list to its sorted() copy to determine if it is already sorted.

After this program

If the idea is still fuzzy, read Day 5: Lists and List Methods in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 list programs, or see every topic on Python exercises.