title: Modifying a set while iterating over it
body: <p>I would like to iterate over a set while I'm deleting
  items from it. There are similar questions for <a href="https://stackoverflow.com/questions/16551334/delete-items-from-a-set-while-iterating-over-it">deleting
  one item at the time</a> or for <a href="https://stackoverflow.com/questions/1207406/remove-items-from-a-list-while-iterating-in-python">lists</a>, but they do
  not work for my case.</p>
  
  <p>The code follows; I iterate over a
  set <code>ZN</code> and in the end of the iteration I'm removing a 
  few items (the ones belonging to the set <code>temp</code>). But the
  iteration is still happening over the "original" ZN.</p>
  
  <p>How can I modify this code to change the set ZN while
  I'm iterating over it?</p>
  
  <pre><code>def CyclotomicCosets(q,n):
      N=q^n-1
      ZN=set(range(N))
      Cosets=[]
      for i in ZN:
          tmp=set([])
          for j in range(n):
              tmp.add( i*(q^j) %N)
          Cosets.append(list(tmp))
          ZN=ZN.difference(tmp) # &lt;------------ Does not do what I want
      return(Cosets)
  </code></pre>