Check if List is Sorted

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

PythonBeginner
Python
# Program to check if a list is sorted

numbers = list(map(float, input("Enter numbers separated by space: ").split()))

if numbers == sorted(numbers):
    print("List is sorted in non-decreasing order.")
else:
    print("List is not sorted.")

Output

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

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