Find indices of the elements smaller than x in a numpy array

numpy, python

Solution

You can use `numpy.flatnonzero` on the boolean mask and Return indices that are non-zero in the flattened version of a:

np.flatnonzero(arr < 6)
# array([1, 2, 3, 5, 6])

Another option on 1d array is `numpy.where`:

np.where(arr < 6)[0]
# array([1, 2, 3, 5, 6])

Problem

Assuming that I have a numpy array such as: ``` import numpy as np arr = np.array([10,1,2,5,6,2,3,8]) ``` How could I extract an array containing the indices of the elements smaller than 6 so I get the following result: ``` np.array([1,2,3,5,6]) ``` I would like something that behave like np.nonzero() but instead of testing for nonzero value, it test for value smaller than x

Original source