Say now I have a numpy array which is defined as,


Now I want to have a list that contains all the indices of the missing values, which is [(1,2),(2,0)] at this case.

Is there any way I can do that?

np.isnan combined with np.argwhere

x = np.array([[1,2,3,4],


array([[1, 2],
       [2, 0]])

You can use np.where to match the boolean conditions corresponding to Nan values of the array and map each outcome to generate a list of tuples.

>>>list(map(tuple, np.where(np.isnan(x))))
[(1, 2), (2, 0)]

Since x!=x returns the same boolean array with np.isnan(x) (because np.nan!=np.nan would return True), you could also write:


However, I still recommend writing np.argwhere(np.isnan(x)) since it is more readable. I just try to provide another way to write the code in this answer.