 Open in App
Not now

# Python | Check if list is strictly increasing

• Last Updated : 30 Dec, 2022

The test for a 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. The 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 the beginning and list beginning from the first element, so that a check can be performed on all elements.

## Python3

 `# 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))`

Output:

```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

 `# 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))`

Output:

```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 a similar task of cumulation of result.

## Python3

 `# 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))`

Output:

```Original list : [1, 4, 5, 7, 8, 10]
Is list strictly increasing ? : True```

Method #4 : Using sort() and extend() methods

## Python3

 `# Python3 code to demonstrate``# to check for strictly increasing list` `# initializing list``test_list ``=` `[``1``, ``4``, ``5``, ``7``, ``4``, ``10``]` `# printing original lists``print` `(``"Original list : "` `+` `str``(test_list))` `# to check for strictly increasing list``res``=``False``x``=``[]``x.extend(test_list)``test_list.sort()``if``(x``=``=``test_list):``    ``res``=``True``# printing result``print` `(``"Is list strictly increasing ? : "` `+` `str``(res))`

Output

```Original list : [1, 4, 5, 7, 4, 10]
Is list strictly increasing ? : False```

Method #5 : Using stack approach:

One approach to check if a list is strictly increasing is to use a stack data structure. A stack is a Last In, First Out (LIFO) data structure, meaning that the last element added to the stack is the first one to be removed.

Here’s an example of using a stack to check if a list is strictly increasing:

## Python3

 `def` `is_strictly_increasing(lst):``    ``stack ``=` `[]``    ``for` `i ``in` `lst:``        ``if` `stack ``and` `i <``=` `stack[``-``1``]:``            ``return` `False``        ``stack.append(i)``    ``return` `True` `test_list ``=` `[``1``, ``4``, ``5``, ``7``, ``8``, ``10``]``print``(is_strictly_increasing(test_list))  ``# True` `test_list ``=` `[``1``, ``4``, ``5``, ``7``, ``7``, ``10``]``print``(is_strictly_increasing(test_list))  ``# False``#This code is contributed by Edula Vinay Kumar Reddy`

Output

```True
False```

The time complexity of this approach is O(n), as the list is traversed once. The space complexity is also O(n), as the stack may store up to n elements at a time.

My Personal Notes arrow_drop_up