69. Problem to check if a list is sorted or not.

In this Python list program, we will take a user input as a list and check whether a specified list is sorted or not with the help of the below-given steps.

Check if a list is sorted:

Steps to solve the program
  1. Take a list as input.
  2. Create a variable and assign the given list in reverse order to it.
  3. Sort the given list.
  4. Check if both lists are similar i.e. the list is sorted or not using an if-else statement.
  5. If yes print True otherwise print False
				
					#Input lists
list1 = [1, 2, 3, 5, 7, 8, 9]
list2 = list1

#Sorting list
list1.sort()

#Printing output
if list1 == list2:
    print(True)
else:
    print(False)
    
list1 = [3, 5, 1, 6, 8, 2, 4]
list2 = list1
list1.sort()
if list1 == list2:
    print(True)
else:
    print(False)
				
			

Output :

				
					True

False
				
			

Related Articles

Access multiple elements of the specified index

Remove duplicate dictionaries from a given list

Leave a Comment