The test for monotonic sequence is a utility that has manifold applications in mathematics and hence every sphere related to mathematics. As mathematics and Computer Science generally go parallel, mathematical operations such as checking for strictly increasing sequence can be useful to gather knowledge of. Same argument can be extended for strictly decreasing lists also. Lets discuss certain ways to perform this test.
Method #1 : Using all() + zip()
The all() generally checks for all the elements fed to it. The task of zip() is to link list beginning from beginning and list beginning from first element, so that a check can be performed on all elements.
# Python3 code to demonstrate # to check for strictly increasing list # using zip() + all() # initializing list test_list = [1, 4, 5, 7, 8, 10] # printing original lists print ("Original list : " + str(test_list)) # using zip() + all() # to check for strictly increasing list res = all(i < j for i, j in zip(test_list, test_list[1:])) # printing result print ("Is list strictly increasing ? : " + str(res)) |
Original list : [1, 4, 5, 7, 8, 10] Is list strictly increasing ? : True
Method #2 : Using reduce() + lambda
reduce() coupled with lambda can also perform this task of checking for monotonicity. reduce function is used to cumulate the result as True or False, lambda function checks for each index value with next index value.
# Python3 code to demonstrate # to check for strictly increasing list # using reduce() + lambda # initializing list test_list = [1, 4, 5, 7, 8, 10] # printing original lists print ("Original list : " + str(test_list)) # using reduce() + lambda # to check for strictly increasing list res = bool(lambda test_list: reduce(lambda i, j: j if i < j else 9999, test_list) != 9999) # printing result print ("Is list strictly increasing ? : " + str(res)) |
Original list : [1, 4, 5, 7, 8, 10] Is list strictly increasing ? : True
Method #3 : Using itertools.starmap() + zip() + all()
Yet another method to perform this task, starmap() works in binding the operation with the zipped lists as done in method 1 and all() also performs similar task of cumulation of result.
# Python3 code to demonstrate # to check for strictly increasing list # using itertools.starmap() + zip() + all() import operator import itertools # initializing list test_list = [1, 4, 5, 7, 8, 10] # printing original lists print ("Original list : " + str(test_list)) # using itertools.starmap() + zip() + all() # to check for strictly increasing list res = all(itertools.starmap(operator.le, zip(test_list, test_list[1:]))) # printing result print ("Is list strictly increasing ? : " + str(res)) |
Original list : [1, 4, 5, 7, 8, 10] Is list strictly increasing ? : True
Recommended Posts:
- Python | Find groups of strictly increasing numbers in a list
- Python | Reverse sequence of strictly increasing integers in a list
- Python - Test for strictly decreasing list
- Python - Check if List is K increasing
- Python List Comprehension | Sort even-placed elements in increasing and odd-placed in decreasing order
- Python | Increasing alternate element pattern in list
- Python - List of N size increasing lists
- Print all Increasing Subsequence of a List
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- Python | Check if a nested list is a subset of another nested list
- Python | Check if a list exists in given list of lists
- Python | Check if a list is contained in another list
- Python - Check List elements from Dictionary List
- Python program for Longest Increasing Subsequence
- Python | Sort Tuples in Increasing Order by any key
- Python program to check if the list contains three consecutive common numbers in Python
- Count no. of columns that are not sorted in increasing order
- Sort even and odd placed elements in increasing order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

