Check if List is Sorted
Check whether a list is sorted in non-decreasing order.
BeginnerTopic: List Programs
Python Check if List is Sorted Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# 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.
Understanding Check if List is Sorted
We compare the list to its sorted() copy to determine if it is already sorted.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.