Open In App

Check if List is Strictly Increasing - Python

Last Updated : 11 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a list of numbers and our task is to check whether the list is strictly increasing meaning each element must be greater than the previous one. For example: a = [1, 3, 5, 7] this is strictly increasing and b = [1, 3, 3, 7], this is not strictly increasing as 3 appears twice.

Using all() with zip()

all() function checks if all conditions in an iterable are True and zip() pairs adjacent elements for comparison.

Python
a = [1, 3, 5, 7]
res = all(x < y for x, y in zip(a, a[1:]))

print(res)  

Output
True

Explanation:

  • zip(a, a[1:]) pairs consecutive elements: (1,3), (3,5), (5,7).
  • generator x < y ensures that each number is strictly less than the next.
  • all(...) returns True only if all comparisons hold.

Using itertools.pairwise()

pairwise() function from itertools generates consecutive element pairs, making comparisons clean and efficient.

Python
from itertools import pairwise  

a = [1, 3, 5, 7]
res = all(x < y for x, y in pairwise(a))

print(res)  

Output
True

Explanation:

  • pairwise(a) generates adjacent pairs: (1,3), (3,5), (5,7).
  • all(x < y for x, y in pairwise(a)) checks if each element is less than the next.

Using a Loop

A for loop can be used to compare adjacent elements and check if they are strictly increasing.

Python
a = [1, 3, 5, 7]
res = True  

for i in range(len(a) - 1):
    if a[i] >= a[i + 1]:
        res = False
        break  

print(res)  

Output
True

Explanation:

  • We iterate through the list using range(len(a) - 1), checking adjacent elements.
  • If a[i] >= a[i+1], the sequence is not strictly increasing, so we set res = False and break the loop.

Using sorted()

We can check if the list is strictly increasing by comparing it with its sorted version and we sort the list using sorted().

Python
a = [1, 3, 5, 7]
res = a == sorted(a) and len(set(a)) == len(a)

print(res)  

Output
True

Explanation:

  • sorted(a) returns a sorted version of a. If a is already sorted, the condition holds.
  • len(set(a)) == len(a) ensures no duplicate elements, making it strictly increasing.

Next Article
Practice Tags :

Similar Reads