The way you're currently doing it, the efficiency is O(N⋅M), which isn't very efficient.
You could try:
def find_nearest_vectorized(a, b): a = a[:, np.newaxis] diff = np.abs(a - b) indexes = np.argmin(diff, axis=0) return indexes