title: Do iterators save memory in Python?
body: <p>I don't quite understand how iterators have memory in Python.</p>
  
  <pre><code>&gt;&gt;&gt; l1 = [1, 2, 3, 4, 5, 6]
  &gt;&gt;&gt; l2 = [2, 3, 4, 5, 6, 7]
  &gt;&gt;&gt; iz = izip(l1, l2)
  </code></pre>
  
  <p>We still require <code>O(min(l1, l2))</code> memory as we need to load the lists <code>l1</code> and <code>l2</code> in memory.</p>
  
  <p>I thought one of the main uses of iterators was to save memory - yet it does not seem to be useful here. </p>
  
  <p>Similarly, the code below is unclear to me:</p>
  
  <pre><code>&gt;&gt;&gt; l1 = ( n for n in [1, 2, 3, 4, 5, 6] )
  &gt;&gt;&gt; l2 = ( n for n in [2, 3, 4, 5, 6, 7] )
  &gt;&gt;&gt; iz = izip(l1, l2)
  </code></pre>
  
  <p>We need to load the lists before converting them into generators, right? This means we'll waste memory. So - what is the point of generators here as well. </p>
  
  <p>This is the only case that makes sense to me:</p>
  
  <pre><code>def build_l1():
      for n in xrange(1, 6):
         yield n
  
  def build_l2:
      for n in xrange(2, 7):
         yield n
  
  l1 = build_l1()
  l2 = build_l2()
  iz = izip(l1, l2)
  </code></pre>
  
  <p>None of the arrays is being loaded into memory. Hence we're in <code>O(1)</code> memory.</p>
  
  <p>How does the memory usage of the iterator functions in Python work? The first two cases seem to use <code>O(min(l1, l2))</code> memory. I thought the main point of iterators was to save memory, which makes the first two cases seem useless.</p>