Python | Check if list is sorted or not

02 Dec, 2018
The sorted operation of list is essential operation in many application. But it takes best of O(nlogn) time complexity, hence one hopes to avoid this. So, to check if this is required or not, knowing if list is by default sorted or not, one can check if list is sorted or not. Lets discuss various ways this can be achieved.
Method #1 : Naive methodThe simplest way to check this is run a loop for first element and check if we could find any smaller element than it after that element, if yes, the list is not sorted.
# Python3 code to demonstrate # to check if list is sorted# using naive method   # initializing listtest_list = [1, 4, 5, 8, 10]  # printing original list print ("Original list : " + str(test_list))  # using naive method to # check sorted list flag = 0i = 1while i < len(test_list):    if(test_list[i] < test_list[i - 1]):        flag = 1    i += 1      # printing resultif (not flag) :    print ("Yes, List is sorted.")else :    print ("No, List is not sorted.")
Output :
Original list : [1, 4, 5, 8, 10]
Yes, List is sorted.

Method #2 : Using sort()The new list can be made as a copy of the original list, sorting the new list and comparing with the old list will give us the result if sorting was required to get sorted list or not.
# Python3 code to demonstrate # to check if list is sorted# using sort()  # initializing listtest_list = [10, 4, 5, 8, 10]  # printing original list print ("Original list : " + str(test_list))  # using sort() to # check sorted list flag = 0test_list1 = test_list[:]test_list1.sort()if (test_list1 == test_list):    flag = 1      # printing resultif (flag) :    print ("Yes, List is sorted.")else :    print ("No, List is not sorted.")
Output :
Original list : [10, 4, 5, 8, 10]
No, List is not sorted.

Method #3 : Using sorted()Using the similar analogy as the above method, but does not create a new space, but just a momentary space for that time and hence useful, shorter and faster method than above.
# Python3 code to demonstrate # to check if list is sorted# using sorted()  # initializing listtest_list = [1, 4, 5, 8, 10]  # printing original list print ("Original list : " + str(test_list))  # using sorted() to # check sorted list flag = 0if(test_list == sorted(test_list)):    flag = 1      # printing resultif (flag) :    print ("Yes, List is sorted.")else :    print ("No, List is not sorted.")
Output :
Original list : [1, 4, 5, 8, 10]
Yes, List is sorted.

Method #4 : Using all()Most elegant, pythonic and faster way to check for sorted list is the use of all(). This uses the similar method as naive, but use of all() make it quicker.
# Python3 code to demonstrate # to check if list is sorted# using all()  # initializing listtest_list = [9, 4, 5, 8, 10]  # printing original list print ("Original list : " + str(test_list))  # using all() to # check sorted list flag = 0if(all(test_list[i] <= test_list[i + 1] for i in range(len(test_list)-1))):    flag = 1      # printing resultif (flag) :    print ("Yes, List is sorted.")else :    print ("No, List is not sorted.")
Output :
Original list : [9, 4, 5, 8, 10]
No, List is not sorted.

Python list-programs
python-list
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Iterate over a list in Python
Rotate axis tick labels in Seaborn and Matplotlib
Enumerate() in Python
Deque in Python
Stack in Python
sum() function in Python
Print lists in Python (5 Different Ways)
Different ways to create Pandas Dataframe
Queue in Python
Read a file line by line in Python