This is because you naively iterate over your data, and loops are slow in python. Instead, use sklearn pairwise distance functions, or even better - use sklearn efficient nearest neighbour search (like BallTree or KDTree). If you do not want to use sklearn, there is also a module in scipy. Finally you can do "matrix tricks" to compute this, since
|| x - y ||^2 = <x-y, x-y> = <x,x> +<y,y> - 2<x,y>
you can do (assuming your data is in matrix form given as X and Y):
X2 = (X**2).sum(axis=1).reshape((-1, 1))Y2 = (Y**2).sum(axis=1).reshape((1, -1))distances = np.sqrt(X2 + Y2 - 2*X.dot(Y.T))