title: Remove circular references in dicts, lists, tuples
body: <p>I have this following really hack code which removes circular references from any kind of data structure built out of <code>dict</code>, <code>tuple</code> and <code>list</code> objects.</p>
  
  <pre><code>import ast
  
  def remove_circular_refs(o):
      return ast.literal_eval(str(o).replace("{...}", 'None'))
  </code></pre>
  
  <p>But I don't like how hacky it is. Can this be done without turning the data structure into a string representation?</p>
  
  <p>Here is an example structure to test with:</p>
  
  <pre><code>doc1 = {
      "key": "value",
      "type": "test1",
  }
  doc1["self"] = doc1
  doc = {
      'tags': 'Stackoverflow python question',
      'type': 'Stackoverflow python question',
  }
  doc2 = {
      'value': 2,
      'id': 2,
  }
  remove_circular_refs(doc)
  remove_circular_refs(doc1)
  remove_circular_refs(doc2)
  </code></pre>