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
Run in browser1# Program to check if a list is sorted23numbers = list(map(float, input("Enter numbers separated by space: ").split()))45if 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.