Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if list is sorted or not in Python
Lists are the most widely used data collections in Python. We may come across situations when we need to know if the given list is already sorted or not. In this article we will see different approaches to achieve this.
Using sorted() Function
We can compare the original list with its sorted version using the sorted() function. If they are equal, the list is already sorted ?
numbers = [11, 23, 42, 51, 67]
print("Given list:", numbers)
if numbers == sorted(numbers):
print("Yes, List is sorted.")
else:
print("No, List is not sorted.")
# Checking with an unsorted list
unsorted_numbers = [11, 23, 21, 51, 67]
print("Given list:", unsorted_numbers)
if unsorted_numbers == sorted(unsorted_numbers):
print("Yes, List is sorted.")
else:
print("No, List is not sorted.")
Given list: [11, 23, 42, 51, 67] Yes, List is sorted. Given list: [11, 23, 21, 51, 67] No, List is not sorted.
Using all() with Generator Expression
We can use the all() function to check if every element is smaller than or equal to the next element. This approach iterates through adjacent pairs ?
numbers = [11, 23, 42, 51, 67]
print("Given list:", numbers)
if all(numbers[i] <= numbers[i + 1] for i in range(len(numbers) - 1)):
print("Yes, List is sorted.")
else:
print("No, List is not sorted.")
# Checking with an unsorted list
unsorted_numbers = [11, 23, 21, 51, 67]
print("Given list:", unsorted_numbers)
if all(unsorted_numbers[i] <= unsorted_numbers[i + 1] for i in range(len(unsorted_numbers) - 1)):
print("Yes, List is sorted.")
else:
print("No, List is not sorted.")
Given list: [11, 23, 42, 51, 67] Yes, List is sorted. Given list: [11, 23, 21, 51, 67] No, List is not sorted.
Using a Custom Function
We can create a reusable function to check if a list is sorted. This makes the code more modular and readable ?
def is_sorted(data):
"""Check if a list is sorted in ascending order."""
return all(data[i] <= data[i + 1] for i in range(len(data) - 1))
# Test with different lists
test_lists = [
[11, 23, 42, 51, 67],
[11, 23, 21, 51, 67],
[1, 2, 2, 3, 4],
[5, 4, 3, 2, 1]
]
for lst in test_lists:
result = "sorted" if is_sorted(lst) else "not sorted"
print(f"List {lst} is {result}")
List [11, 23, 42, 51, 67] is sorted List [11, 23, 21, 51, 67] is not sorted List [1, 2, 2, 3, 4] is sorted List [5, 4, 3, 2, 1] is not sorted
Comparison of Methods
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
sorted() comparison |
O(n log n) | O(n) | Simple one-time checks |
all() with generator |
O(n) | O(1) | Large lists, memory efficiency |
| Custom function | O(n) | O(1) | Reusable code, multiple checks |
Conclusion
The all() method with generator expression is the most efficient approach with O(n) time complexity and O(1) space complexity. Use sorted() comparison for simple cases, but prefer the generator approach for better performance with large lists.
