all_monotone

iteration_utilities.all_monotone(iterable, decreasing=False, strict=False)

Checks if the elements in iterable are (strictly) monotonic increasing or decreasing.

Parameters:
iterableiterable

Any iterable to test.

decreasingbool, optional

If False check if the values are monotonic increasing, otherwise check for monotone decreasing. Default is False.

strictbool, optional

If True check if the elements are strictly greater or smaller (> or <) than their predecessor. Otherwise use >= and <=.

Returns:
monotonicbool

True if all elements in iterable are monotonic or False if not.

Notes

If the input is empty the function returns True.

Examples

This is roughly equivalent to all(itertools.starmap(operator.lt, iteration_utilities.successive(iterable, 2))) with the appropriate operator depending on decreasing and strict:

>>> from iteration_utilities import all_monotone
>>> all_monotone([1,1,1,1,1,1,1,1,1])
True
>>> all_monotone([1,1,1,1,1,1,1,1,1], strict=True)
False
>>> all_monotone([2,1,1,1,1,1,1,1,0], decreasing=True)
True
>>> all_monotone([2,1,1,1,1,1,1,1,0], decreasing=True, strict=True)
False