Numpy: find first index of value fast

cyborg picture cyborg · Oct 3, 2011 · Viewed 89.3k times · Source

How can I find the index of the first occurrence of a number in a Numpy array? Speed is important to me. I am not interested in the following answers because they scan the whole array and don't stop when they find the first occurrence:

itemindex = numpy.where(array==item)[0][0]
nonzero(array == item)[0][0]

Note 1: none of the answers from that question seem relevant Is there a Numpy function to return the first index of something in an array?

Note 2: using a C-compiled method is preferred to a Python loop.

Answer

cyborg picture cyborg · Oct 5, 2011

There is a feature request for this scheduled for Numpy 2.0.0: https://github.com/numpy/numpy/issues/2269