How to compare fast the value with the ones of neighbours in a 3D array
NumPy performs operations in a vectorised manner, you should try and operate on the whole array and avoid explicit loops.

e.g. np.argwhere( (test == 1) &amp; np.roll( test==2 , shift=-1 , axis=0 ) )

can be done for different shift and axis values and results combined