
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if list is strictly increasing in Python
Given a list, we may need to check for the sequence of its elements. In this article we will find out if the elements present in the list are in a strictly increasing order. Below programs achieve that objective.
With all and zip
In this approach we first slice each element compare its value to the next element that is sliced. If all such comparisons hold true then we conclude the list is strictly in increasing order.
Example
listA = [11,23,42,51,67] #Given list print("Given list : ",listA) # Apply all and range if (all(i < j for i, j in zip(listA, listA[1:]))): print("Yes, List is sorted.") else: print("No, List is not sorted.") # Checking again listB = [11,23,21,51,67] print("Given list : ",listB) # Apply all and range if (all(i < j for i, j in zip(listB, listB[1:]))): print("Yes, List is sorted.") else: print("No, List is not sorted.")
Output
Running the above code gives us the following result −
Given list : [11, 23, 42, 51, 67] Yes, List is sorted. Given list : [11, 23, 21, 51, 67] No, List is not sorted.
with itertools.starmap
The makes an iterator that computes the function using arguments obtained from the iterable. We zip the list of elements after slicing them one by one and then taking through the less than equal to operator. Please note we have used strings instead of numbers in the below exmaples.
Example
import operator import itertools listA = ['Mon','Tue','Sun'] #Given list print("Given list : ",listA) # Apply all and range if all(itertools.starmap(operator.le, zip(listA, listA[1:]))): print("Yes, List is sorted.") else: print("No, List is not sorted.") # Checking again listB = ['Mon','Sun','Tue'] print("Given list : ",listB) # Apply all and range if all(itertools.starmap(operator.le, zip(listB, listB[1:]))): print("Yes, List is sorted.") else: print("No, List is not sorted.")
Output
Running the above code gives us the following result −
Given list : ['Mon', 'Tue', 'Sun'] No, List is not sorted. Given list : ['Mon', 'Sun', 'Tue'] Yes, List is sorted.
- Related Articles
- Program to check whether list is strictly increasing or strictly decreasing in Python
- A strictly increasing linked list in Python
- Find groups of strictly increasing numbers in a list in Python
- Check if it is possible to make two matrices strictly increasing by swapping corresponding values only in Python
- Strictly increasing sequence JavaScript
- Make Array Strictly Increasing in C++
- Count Strictly Increasing Subarrays in C++
- Program to find length of contiguous strictly increasing sublist in Python
- Python - Check if a list is contained in another list
- Strictly increasing or decreasing array - JavaScript
- Program to check sublist sum is strictly greater than the total sum of given list Python
- Find Maximum Sum Strictly Increasing Subarray in C++
- Check if list is sorted or not in Python
- Program to count n digit integers where digits are strictly increasing in Python
- Program to find length of longest strictly increasing then decreasing sublist in Python
