title: Python Optimized Comparison Between List of Dict
body: <p>I'm trying to see whether nodes reside within the volume of a sphere, and add the node id to a list. However, the efficiency of the algorithm is incredibly slow and I'm not sure how to improve it.</p>
  
  <p>I have two lists. List A has the format <strong>[{'num': ID, 'x': VALUE, 'y': VALUE, 'z': VALUE]</strong> while List B has the format <strong>[{'x': VALUE, 'y': VALUE, 'z': VALUE, 'rad': VALUE}]</strong>.</p>
  
  <p>The size of both lists can run upwards of 100,000 items each.</p>
  
  <p>My current code is posted below, but it's very inefficient. </p>
  
  <pre><code>    filteredList = []
      for i in range(len(sList)):
  
              minx = (sList[i]['x']) - (sList[i]['radius'])
              maxx = (sList[i]['x']) + (sList[i]['radius'])
              miny = (sList[i]['y']) - (sList[i]['radius'])
              maxy = (sList[i]['y']) + (sList[i]['radius'])
              minz = (sList[i]['z']) - (sList[i]['radius'])
              maxz = (sList[i]['z']) + (sList[i]['radius'])
  
              for j in range(len(nList)):
                      if minx &lt;= nList[j]['x'] &lt;= maxx:
                              if miny &lt;= nList[j]['y'] &lt;= maxy:
                                      if minz &lt;= nList[j]['z'] &lt;= maxz:
                                              tmpRad = findRadius(sList[i],nList[j])
                                              if tmpRad &lt;= sList[i]['radius']:
                                                      filteredList.append(int(nList[j]['num']))
  </code></pre>
  
  <p>I'm at a loss and appreciate any ideas.</p>
  
  <p><strong>Edit:</strong> Adding extra information about the data format.</p>
  
  <p><strong>List A</strong> (nList) -- defines nodes, with locations x,y,z, and identifier num<br>
  [{'y': 0.0, 'x': 0.0, 'num': 1.0, 'z': 0.0},<br>
  {'y': 0.0, 'x': 1.0, 'num': 2.0, 'z': 0.0},<br>
  {'y': 0.0, 'x': 2.0, 'num': 3.0, 'z': 0.0},<br>
  {'y': 0.0, 'x': 3.0, 'num': 4.0, 'z': 0.0},<br>
  {'y': 0.0, 'x': 4.0, 'num': 5.0, 'z': 0.0},<br>
  {'y': 0.0, 'x': 5.0, 'num': 6.0, 'z': 0.0},<br>
  {'y': 0.0, 'x': 6.0, 'num': 7.0, 'z': 0.0},<br>
  {'y': 0.0, 'x': 7.0, 'num': 8.0, 'z': 0.0},<br>
  {'y': 0.0, 'x': 8.0, 'num': 9.0, 'z': 0.0},<br>
  {'y': 0.0, 'x': 9.0, 'num': 10.0, 'z': 0.0}]  </p>
  
  <p><strong>List B</strong> (sList) -- defines spheres using x,y,z, radius<br>
  [{'y': 18.0, 'x': 25.0, 'z': 26.0, 'radius': 0.0056470000000000001},<br>
  {'y': 29.0, 'x': 23.0, 'z': 45.0, 'radius': 0.0066280000000000002},<br>
  {'y': 46.0, 'x': 29.0, 'z': 13.0, 'radius': 0.014350999999999999},<br>
  {'y': 0.0, 'x': 20.0, 'z': 25.0, 'radius': 0.014866000000000001},<br>
  {'y': 27.0, 'x': 31.0, 'z': 18.0, 'radius': 0.018311999999999998},<br>
  {'y': 10.0, 'x': 36.0, 'z': 46.0, 'radius': 0.024702000000000002},<br>
  {'y': 27.0, 'x': 13.0, 'z': 48.0, 'radius': 0.027300999999999999},<br>
  {'y': 14.0, 'x': 1.0, 'z': 13.0, 'radius': 0.033889000000000002},<br>
  {'y': 31.0, 'x': 20.0, 'z': 11.0, 'radius': 0.034118999999999997},<br>
  {'y': 23.0, 'x': 28.0, 'z': 8.0, 'radius': 0.036683}]  </p>