title: StopIteration in generators
body: <p>I'm learning python's generators, iterators, iterables, and I can't explain why the following is not working. I want to create, as an exercise, a simple version of the function zip. Here's what i did:</p>
  
  <pre><code>def myzip(*collections):
  
      iterables = tuple(iter(collection) for collection in collections)
  
      yield tuple(next(iterable) for iterable in iterables)
  
  test = myzip([1,2,3],(4,5,6),{7,8,9})
  
  print(next(test))
  print(next(test))
  print(next(test))
  </code></pre>
  
  <p>What I do is:</p>
  
  <ul>
  <li>I have <code>collections</code> which is a tuple of some collections</li>
  <li>I create a new tuple <code>iterables</code> where, for each collection (which is iterable), I get the iterator using <code>iter</code></li>
  <li>Then, I create a new tuple where, on each iterable, I call <code>next</code>. This tuple is then yield.</li>
  </ul>
  
  <p>So I expect that at the first execution the object <code>iterables</code> is created (and stored). Then in each iteration (including the first one) I call <code>next</code> on every iterable stored before and return it.</p>
  
  <p>However this is what I get:</p>
  
  <pre><code>(1, 4, 8)
  ---------------------------------------------------------------------------
  StopIteration                             Traceback (most recent call last)
  &lt;ipython-input-108-424963a58e58&gt; in &lt;module&gt;()
        8 
        9 print(next(test))
  ---&gt; 10 print(next(test))
  
  StopIteration: 
  </code></pre>
  
  <p>So I see that the first iteration is fine and the result is correct. However, the second iteration raise a StopIteration exception and I don't understand why: each iterable still has some values, so none of the <code>next</code>s return StopIteration. In fact, this works:</p>
  
  <pre><code>def myziptest(*collections):
  
      iterables = tuple(iter(collection) for collection in collections)
  
      for _ in range(3):
          print(tuple(next(iterable) for iterable in iterables))
  
  test = myziptest([1,2,3],(4,5,6),{7,8,9})
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>(1, 4, 8)
  (2, 5, 9)
  (3, 6, 7)
  </code></pre>
  
  <p>So what is going on?
  Thanks a lot</p>