Brute force closest pair algorithms - for loops
Question: New to Python. Trying to analyze the algorithm for Closest pair of points.
Found an [example](http://rosettacode.org/wiki/Closest-pair_problem#Python)

Which has these lines:

    
    
    return min( ((abs(point[i] - point[j]), (point[i], point[j]))
                     for i in range(numPoints-1)
                     for j in range(i+1,numPoints)),
                    key=itemgetter(0))
    

It is not clear to me how the for loops are being evaluated.

How are the for loops related to the points, key and itemgetter?

When I put this code in [Ideone](http://ideone.com/1u27f9)

I get a runtime error in the times() function:

    
    
    def times():
        ''' Time the different functions
        '''
        import timeit
    
        functions = [bruteForceClosestPair, closestPair]
        for f in functions:
            print 'Time for', f.__name__, timeit.Timer(
                '%s(pointList)' % f.__name__,
                'from closestpair import %s, pointList' % f.__name__).timeit(number=1)
    

Thank you.


Answer: Your `min` code is equivalent to using a
[generator](http://docs.python.org/2/tutorial/classes.html#generators) as
follows:

    
    
    def getPoints(point, numPoints):
      for i in range(numPoints - 1):
        for j in range(i + 1, numPoints):
          yield (abs(point[i] - point[j]), (point[i], point[j]))
    
    return min(getPoints(point, numPoints), key=itemgetter(0))
    

As @DSM noted, the first argument to `min` in your code is a [generator
expression](http://docs.python.org/2/tutorial/classes.html#generator-
expressions).

