title: 'RuntimeError: maximum recursion depth exceeded in cmp' when working with lists
body: <p>I've encountered the error <code>RuntimeError: maximum recursion depth exceeded in cmp</code> when working with lists. More precisely, <code>p0 in points</code>, the <code>points.index(p0)</code> method call as well as the <code>points.remove(p0)</code> method call on the <code>points</code> list have been raising the error for a specific dictionary <code>p0</code> at a specific index of my <code>points</code> list. The <code>points</code>list contains 4700 dictionaries at the time of the error, reduced one dictionary by one from a list of 12000 objects until the error is raised. The <code>p0</code> dictionary contains a reference to another dictionary in the list, which in returns contains a reference to the <code>p0</code> object. The <code>p0</code> dictionary as well as the dictionary it contains a reference to appear twice in the <code>points</code> list before the error is raised by any of the three method call.</p>
  
  <p>Where does this error come from?</p>
  
  <p><strong>EDIT</strong>: Here's the code that raises the error.</p>
  
  <pre><code>for roadType in roadTypes:
      points = roadPoints[roadType][:]
  
      while len(roadTails[roadType]) &gt; 0:
          p0 = roadTails[roadType].pop()
  
          p1 = p0['next']
          points.remove(p0) # Where the error occurs
          points.remove(p1)
  
          while True:
              p2 = find(p1, points, 0.01)
  
              if p2:
                  points.remove(p2)
  
                  p3 = p2['next']
                  points.remove(p3)
  
                  if p3 in roadTails[roadType]:
                      roadTails[roadType].remove(p3)
                      break
                  else:
                      p0, p1 = p2, p3
                      continue
  
              else: break
  </code></pre>
  
  <p>Here's the definition of <code>find</code>, where <code>dist</code> calculates the distance between two points.</p>
  
  <pre><code>def find(p1, points, tolerance = 0.01):
      for p2 in points:
          if dist(p2['coords'], p1['coords']) &lt;= tolerance:
              return p2
      return False
  </code></pre>
  
  <p>Here's the full traceback of the error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "app.py", line 314, in &lt;module&gt;
      points.remove(p0) # Where the error occurs
  RuntimeError: maximum recursion depth exceeded in cmp
  </code></pre>