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
- Take a list as input.
- Create a variable and assign the given list in reverse order to it.
- Sort the given list.
- Check if both lists are similar i.e. the list is sorted or not using an if-else statement.
- 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
Python program to remove duplicate dictionaries from a given list.
Python program to check if the elements of a given list are unique or not.
Python program to remove duplicate sublists from the list.
Python program to create a list by taking an alternate item from the list.
Python program to remove duplicate tuples from the list.
Python program to insert an element before each element of a list.