# Python | Check if list is strictly increasing

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 sequences can be useful to gather knowledge. The same argument can be extended for strictly decreasing lists also. Let’s 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.

Approach:

1. Initialize a list called “test_list” with some integer elements.
2. Print the original list.
3. Use the “zip()” function to iterate through the list and create pairs of adjacent elements.
4. Use the “all()” function to check if all pairs of adjacent elements satisfy the condition i < j, where i and j are the two elements in the pair.
5. Store the result in a variable called “res“.
6. Print the result, along with a message indicating if the list is strictly increasing or not.

Below is the implementation of the above approach:

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

Time Complexity: O(n) where n is the length of the list.
Auxiliary Space: O(1)

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

Time Complexity: O(n)
Auxiliary Space: O(1)

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

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(1), as the algorithm uses only a constant amount of extra space.

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

Time complexity: O(nlogn) due to the sorting operation.
Auxiliary space: O(n) due to the creation of a copy of the original list using extend().

Method #5: Using stacks

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: O(n), as the list is traversed once.
The space complexity: O(n), as the stack may store up to n elements at a time.

Method #6 : Using numpy()

1. Convert the input list to a NumPy array.
2. Calculate the difference between adjacent elements of the array using the np.diff() function.
3. Check if all elements of the resulting array are greater than 0 using the np.all() function.
4. Return True if all elements are greater than 0, otherwise return False.

## Python3

 `import` `numpy as np`   `# function to check if a list is strictly increasing using numpy`     `def` `is_increasing_6(lst):` `  `  `    ``# Converting input list to a numpy array` `    ``arr ``=` `np.array(lst)`   `    ``# calculate the difference between adjacent elements of the array` `    ``diff ``=` `np.diff(arr)`   `    ``# check if all differences are positive` `    ``# using the np.all() function` `    ``is_increasing ``=` `np.``all``(diff > ``0``)`   `    ``# return the result` `    ``return` `is_increasing`     `# Input list` `test_list ``=` `[``1``, ``4``, ``5``, ``7``, ``8``, ``10``]`   `# Printing original lists` `print``(``"Original list : "` `+` `str``(test_list))`   `result ``=` `is_increasing_6(test_list)`   `print``(result)`

Output:

```Original list : [1, 4, 5, 7, 8, 10]
True```

Time complexity: O(n), where n is the length of the input list. This is because the function uses NumPy’s built-in functions, np.diff() and np.all(), to efficiently perform the necessary calculations. These functions operate on the NumPy array created from the input list, and their time complexity is proportional to the length of the array. However, NumPy is highly optimized for numerical operations, so the actual time taken by these functions is usually much lower than a traditional loop-based approach.
Auxiliary space: O(n), where n is the length of the input list. This is because the function creates a NumPy array to store the input list, and the size of this array is proportional to the length of the input list. Additionally, the np.diff() function creates another array to store the differences between adjacent elements, which is also proportional to the length of the input list. However, the additional memory usage is negligible compared to the input list itself, so the space complexity is still considered O(1) in practice.

### Method 7: Using itertools’s pairwise() method and all() method with comparison operator:

all(a < b for a, b in pairwise(my_list))

Algorithm:

1. Define a function named is_strictly_increasing which takes one argument, a list of integers.
2. Import pairwise method from itertools.
3. Use all() method with a comparison operator and pass the pairwise(my_list) to it. It will check if all the pairs are strictly increasing or not.
4. If yes, then return True, else return False.

Example:

## Python3

 `from` `itertools ``import` `pairwise`   `# Function ` `def` `is_strictly_increasing(my_list):` `  `  `    ``# using pairwise method to iterate through the list and ` `    ``# create pairs of adjacent elements.` `    `  `    ``# all() method checks if all pairs of adjacent elements ` `    ``# satisfy the condition i < j, where i and j ` `    ``# are the two elements in the pair.` `    ``if` `all``(a < b ``for` `a, b ``in` `pairwise(my_list)):` `        ``return` `True` `    ``else``:` `        ``return` `False`   `# Initializing list` `test_list ``=` `[``1``, ``4``, ``5``, ``7``, ``8``, ``10``]`   `# Printing original lists` `print` `(``"Original list : "` `+` `str``(test_list))`   `# Checking for strictly increasing list ` `# using itertools pairwise() and all() method` `res ``=` `is_strictly_increasing(test_list)`   `# Printing the result` `print` `(``"Is list strictly increasing ? : "` `+` `str``(res))`

Output:

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

Time Complexity: O(n), where n is the length of the input list. The pairwise method takes O(n) time to iterate through the list and create pairs of adjacent elements, and all method takes O(n) time to check if all pairs of adjacent elements satisfy the condition i < j.
Auxiliary Space: O(1), as we are not using any extra space, only two variables are used in the program.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next