title: iterating over a growing set in python
body: <p>I have a set, setOfManyElements, which contains n elements. I need to go through all those elements and run a function on each element of S: </p>
  
  <pre><code>for s in setOfManyElements:
     elementsFound=EvilFunction(s)
     setOfManyElements|=elementsFound
  </code></pre>
  
  <p>EvilFunction(s) returns the set of elements it has found. Some of them will already be in S, some will be new, and some will be in S and will have already been tested.</p>
  
  <p>The problem is that each time I run EvilFunction, S will expand (until a maximum set, at which point it will stop growing). So I am essentially iterating over a growing set. Also EvilFunction takes a long time to compute, so you do not want to run it twice on the same data.</p>
  
  <p>Is there an efficient way to approach this problem in Python 2.7?</p>
  
  <p>LATE EDIT: changed the name of the variables to make them more understandable. Thanks for the suggestion</p>