def closest(position, positions): x0, y0 = position dbest, ibest = None, None for i, (x, y) in enumerate(positions): d = (x - x0) ** 2 + (y - y0) ** 2 if dbest is None or d < dbest: dbest, ibest = d, i return ibest import random positions = [(random.random(), random.random()) for _ in xrange(10000000)] %timeit closest((.5, .5), positions) %pylab positions = rand(10000000, 2) type(positions) positions.ndim, positions.shape x, y = positions[:,0], positions[:,1] distances = (x - .5) ** 2 + (y - .5) ** 2 %timeit exec In[9] %timeit ibest = distances.argmin()