Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if list is strictly increasing in Python
A strictly increasing list is one where each element is smaller than the next element. Python provides several approaches to check this condition using all() with zip(), comparison operators, or the itertools module.
Using all() and zip()
This approach compares each element with its next element using zip(). The all() function returns True only if all comparisons are True ?
numbers = [11, 23, 42, 51, 67]
print("Given list:", numbers)
# Check if strictly increasing
if all(i < j for i, j in zip(numbers, numbers[1:])):
print("Yes, List is strictly increasing.")
else:
print("No, List is not strictly increasing.")
# Checking a non-increasing list
numbers2 = [11, 23, 21, 51, 67]
print("Given list:", numbers2)
if all(i < j for i, j in zip(numbers2, numbers2[1:])):
print("Yes, List is strictly increasing.")
else:
print("No, List is not strictly increasing.")
Given list: [11, 23, 42, 51, 67] Yes, List is strictly increasing. Given list: [11, 23, 21, 51, 67] No, List is not strictly increasing.
Using itertools.starmap()
The starmap() function applies a function using arguments from an iterable. We can use operator.lt (less than) to check the strictly increasing condition ?
import operator
import itertools
words = ['Apple', 'Banana', 'Cherry']
print("Given list:", words)
# Check if strictly increasing (alphabetically)
if all(itertools.starmap(operator.lt, zip(words, words[1:]))):
print("Yes, List is strictly increasing.")
else:
print("No, List is not strictly increasing.")
# Checking a non-increasing list
words2 = ['Banana', 'Apple', 'Cherry']
print("Given list:", words2)
if all(itertools.starmap(operator.lt, zip(words2, words2[1:]))):
print("Yes, List is strictly increasing.")
else:
print("No, List is not strictly increasing.")
Given list: ['Apple', 'Banana', 'Cherry'] Yes, List is strictly increasing. Given list: ['Banana', 'Apple', 'Cherry'] No, List is not strictly increasing.
Using Simple Comparison
A straightforward approach using a loop to compare adjacent elements ?
def is_strictly_increasing(data):
for i in range(len(data) - 1):
if data[i] >= data[i + 1]:
return False
return True
numbers = [5, 10, 15, 20]
print("Given list:", numbers)
print("Strictly increasing:", is_strictly_increasing(numbers))
numbers2 = [5, 10, 10, 20] # Contains equal elements
print("Given list:", numbers2)
print("Strictly increasing:", is_strictly_increasing(numbers2))
Given list: [5, 10, 15, 20] Strictly increasing: True Given list: [5, 10, 10, 20] Strictly increasing: False
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
all() + zip() |
High | Good | Pythonic one-liner |
itertools.starmap() |
Medium | Good | Functional programming style |
| Simple loop | High | Best | Beginners, large lists |
Conclusion
Use all() with zip() for concise, readable code. The simple loop approach offers the best performance for large lists. Choose itertools.starmap() when working in a functional programming style.
