Return the index of the first element in the list from where incremental increase starts

  indexing, list, python

Suppose I have a list like this, where numbers increase in different steps:

[ 0,  4,  6,  8, 12, 15, 19, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]

I want to return the index for the first element in the list where the increase is incremental (+1 step only). In this case, 23 is the first location from which point the increase becomes incremental, and its index would be 8, which is what I want as an output.

What would be an elegant simple way to achieve this? This is what I have tried:

>>> for (a,b) in zip(l, l[1:]):
...     if b-a == 1:
...             print(l.index(a))
...             break

UPDATE: In this particular setup, once the increase becomes incremental it will continue to stay that way.

Source: Python Questions

LEAVE A COMMENT