["python","list"]
Check list monotonicity

How do I efficiently check list monotonicity? i.e. that it is either a non-decreasing or non-increasing set of ordered values? Examples: [0, 1, 2, 3, 3, 4] # This is a monotonically increasing list [4.3, 4.2, 4.2, -2] # This is a monotonically decreasing list [2, 3, 1] # This is neither
Are repeated values (e.g. [1, 1, 2]) monotonic? If yes: def non_decreasing(L): return all(x<=y for x, y in zip(L, L[1:])) def non_increasing(L): return all(x>=y for x, y in zip(L, L[1:])) def monotonic(L): return non_decreasing(L) or non_increasing(L) If no: def strictly_increasing(L): return all(x<y for x, y in zip(L, L[1:])) def strictly_decreasing(L): return all(x>y for x, y in zip(L, L[1:])) def strictly_monotonic(L): return strictly_increasing(L) or strictly_decreasing(L)