Check if single element is contained in Numpy Array

I timed some methods to do this in python 3.7:

import numpy as np
rnd = np.random.RandomState(42)
one_d = rnd.randint(100, size=10000)
n_d = rnd.randint(100, size=(10000, 10000))
searched = 42

# One dimension
%timeit if np.isin(one_d, searched, assume_unique=True).any(): pass
%timeit if np.in1d(one_d, searched, assume_unique=True).any(): pass
%timeit if searched in one_d: pass
%timeit if one_d[np.searchsorted(one_d, searched)] == searched: pass
%timeit if np.count_nonzero(one_d == searched): pass

print("------------------------------------------------------------------")

# N dimensions
%timeit if np.isin(n_d, searched, assume_unique=True).any(): pass
%timeit if np.in1d(n_d, searched, assume_unique=True).any(): pass
%timeit if searched in n_d: pass
%timeit if np.count_nonzero(n_d == searched): pass
>>> 42.8 µs ± 79.3 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> 38.6 µs ± 76.2 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> 16.4 µs ± 57.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
>>> 4.7 µs ± 62.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
>>> 12.1 µs ± 69.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
>>> ------------------------------------------------------------------
>>> 239 ms ± 1.04 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
>>> 241 ms ± 1.17 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
>>> 156 ms ± 2.78 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
>>> 163 ms ± 527 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

The fastest for 1d arrays is the one proposed above of np.searchsorted however it cannot be used for ndarrays. Also np.count_nonzero is the fastest but it is not much faster than the pythonic in, so I would recommend to use in.


if a is a numpy array:

a = np.array([1, 2])

then use:

1 in a

which returns true, while:

0 in a

returns false


You can use 0 in a. i.e

a = np.array([9,2,7,0])
0 in a