In this tutorial, we will look at how to check if a list is sorted or not in Python with the help of some examples.
How to check if a list is sorted in Python?
To check if a list is sorted, you can use the following methods –
- Compare the list with its corresponding sorted list.
- Iterate through the list and see if the elements are in order.
Let’s now take a look at the above methods in detail with the help of some examples.
Compare the list with corresponding sorted list
To check if a list is sorted in Python, compare the list to the list resulting from a sort operation on the original list. If the list resulting after a sort operation is the same as the original list we can say that the original list is sorted.
You can use the python built-in sorted()
function to get the sorted list. Let’s take a look at an example.
# create a list ls = [1, 2, 3, 4, 5] # check if list is sorted print(ls == sorted(ls))
Output:
True
Here we get True
as the output because the original list ls
is already sorted.
Let’s look at another example.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# create a list ls = [1, 2, 4, 3, 5] # check if list is sorted print(ls == sorted(ls))
Output:
False
Here we get False
as the output because the list ls
does not contain elements in the sorted order.
The worst-case time complexity of this method is O(N Log N) where N is the size of the list.
By Iterating through the list
You can also check if the list is sorted or not by iterating through the list elements. If any element is out of order (for example, it’s smaller than the previous element) we can say that the list is not sorted in ascending order.
Let’s look at an example.
# function to check if list is sorted in ascending order def is_list_sorted(ls): for i in range(1, len(ls)): # return False if the element is smaller than the previous element if ls[i] < ls[i-1]: return False return True # create a list ls = [1, 2, 3, 4, 5] # check if list is sorted print(is_list_sorted(ls))
Output:
True
We get the same result as above. The list ls
is in sorted order and thus we get True
as the output.
Let’s see what happens if we use this method on a list that is not sorted.
# create a list ls = [1, 2, 4, 3, 5] # check if list is sorted print(is_list_sorted(ls))
Output:
False
We get False
as the output.
The worst-case time complexity of this method is O(N) where N is the size of the list.
You can also use the above methods to check if a list is sorted in descending order or not.
# function to check if list is sorted in descending order def is_list_sorted_desc(ls): for i in range(1, len(ls)): # return False if the element is larger than the previous element if ls[i] > ls[i-1]: return False return True # create a list ls = [5, 4, 3, 2, 1] # check if list is sorted print(ls == sorted(ls, reverse=True)) print(is_list_sorted_desc(ls))
Output:
True True
Here we use both the methods to check if the list ls
is sorted in descending order or not.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.