title: Compare the Euclidean distance of two lists of tuples with fewer comparisons python
body: <p>I am trying to calculate the Euclidean distance of two list of tuples with the threshold of 4 . If the threshold is less than a particular value then increment the counter . Each tuple is the x,y,z coordinate of the point . Is there anyway i can lower of the comparison of list1 with list2 .. </p>
  
  <pre><code>  X = [ (1,2,3),(2,3,4), (4,5,6) ]
    Y = [ (1,2,2) , (3,4,5),(6,7,8) ]
    from math import sqrt
    dist_X = [ sqrt((p[0] - 0)**2 + (p[1] - 0)**2 + (p[2] - 0)**2) for p in X]
    dist_Y = [ sqrt((p[0] - 0)**2 + (p[1] - 0)**2 + (p[2] - 0)**2) for p in Y]
    for x in dist_X:
       print (x ,  [ i for i,y in enumerate(dist_Y) if abs(x-y) &lt;= 4])
  </code></pre>
  
  <p>I was thinking of first calculating the Euclidean distance of each point with the origin (0,0,0) so that both the lists now contain the points that are close to each other but it didnt work because its a scalar value .. Am i going in the right direction? </p>
  
  <p>EDIT</p>
  
  <pre><code>   visited1 = [ (1,2,3),(2,3,4), (4,5,6) ]
     visited2   = [ (1,2,2) , (3,4,5),(6,7,8) ]
      def euclidean(a,b):
          return sqrt((a[0] - b[0])**2+(a[1]-b[1])**2+(a[2]-b[2])**2)
     comparison = 0
     for i,j in enumerate(visited2):
       for k,l in enumerate(visited1):
           if euclidean(visited2[i],visited1[k]) &lt; 4:
                   count += 1
           comparison += 1
  </code></pre>
  
  <p>In this every element of the list1 is compared with every element in the list2 .. I want to know if there is a way i can minimise the comparisons given the points(x,y,z) that i have?</p>