title: understanding zip function
body: <p>All discussion is about python 3.1.2; see <a href="http://docs.python.org/py3k/library/functions.html?highlight=zip#zip" rel="noreferrer">Python docs</a> for the source of my question.</p>
  <p>I know what <code>zip</code> does; I just don't understand why it can be implemented like this:</p>
  <pre><code>def zip(*iterables):
      # zip('ABCD', 'xy') --&gt; Ax By
      iterables = map(iter, iterables)
      while iterables:
          yield tuple(map(next, iterables))
  </code></pre>
  <p>Let's say I call <code>zip(c1, c2, c3)</code>. If I understand correctly, iterables is initially the tuple (c1, c2, c3).</p>
  <p>The line <code>iterables = map(iter, iterables)</code> converts it to an iterator that would return iter(c1), iter(c2), iter(c3) if iterated through.</p>
  <p>Inside the loop, <code>map(next, iterables)</code> is an iterator that would return <code>next(iter(c1))</code>, <code>next(iter(c2))</code>, and <code>next(iter(c3))</code> if iterated through. The <code>tuple</code> call converts it to <code>(next(iter(c1)), next(iter(c2)), next(iter(c3))</code>, exhausting its argument (<code>iterables</code>) on the very first call as far as I can tell. I don't understand how the <code>while</code> loop manages to continue given that it checks <code>iterables</code>; and if it does continue why the <code>tuple</code> call doesn't return empty tuple (the iterator being exhausted).</p>
  <p>I'm sure I'm missing something very simple..</p>