top of page

Python Tutorial

Public·1 member

Python | Check if list is sorted or not

Create a copy of the original list and sorting the original list with new create copied list will give us the result if sorting was required to get sorted list or not.


# Python3 code   
# to check if list is sorted 
# using sort()

# initializing list 
list1 = [10, 5, 4, 8, 10]
 
# printing original list  
print ("Original list : " + str(list1)) 

# using sort() to  
# check sorted list  
flag = 0
list2 = list1[:] 
list2.sort() 

if (list2 == list1):
    flag = 1
    
# printing result 
if (flag) : 
    print ("Yes, List is sorted.") 
else : 
    print ("No, List is not sorted.") 


26 Views
bottom of page