def smallest(M, f):
    dst = pdist(M, 'euclidean')
rows, cols = np.triu_indices(M.shape[0], k=1)
idx = np.argsort(dst)[:f]
return np.vstack((dst[idx], rows[idx], cols[idx])).T
huge matrix sorted and then find smallest elements with their indices into a list