<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
<a class="moz-txt-link-abbreviated" href="mailto:opstad@batnet.com">opstad@batnet.com</a> a écrit :
<blockquote
 cite="mid:a227085e-7ab7-4ca8-8899-c22bd7eef87a@n7g2000prc.googlegroups.com"
 type="cite">
  <pre wrap="">I'm a little baffled by the inconsistency here. Anyone have any
explanations?

  </pre>
  <blockquote type="cite">
    <blockquote type="cite">
      <blockquote type="cite">
        <pre wrap="">def gen():
        </pre>
      </blockquote>
    </blockquote>
  </blockquote>
  <pre wrap=""><!---->...   yield 'a'
...   yield 'b'
...   yield 'c'
...
  </pre>
  <blockquote type="cite">
    <blockquote type="cite">
      <blockquote type="cite">
        <pre wrap="">[c1 + c2 for c1 in gen() for c2 in gen()]
        </pre>
      </blockquote>
    </blockquote>
  </blockquote>
  <pre wrap=""><!---->['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']

  </pre>
  <blockquote type="cite">
    <blockquote type="cite">
      <blockquote type="cite">
        <pre wrap="">list(c1 + c2 for c1 in gen() for c2 in gen())
        </pre>
      </blockquote>
    </blockquote>
  </blockquote>
  <pre wrap=""><!---->['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']

  </pre>
  <blockquote type="cite">
    <blockquote type="cite">
      <blockquote type="cite">
        <pre wrap="">it1 = gen()
it2 = gen()
list(c1 + c2 for c1 in it1 for c2 in it2)
        </pre>
      </blockquote>
    </blockquote>
  </blockquote>
  <pre wrap=""><!---->['aa', 'ab', 'ac']

Why does this last list only have three elements instead of nine?

--
<a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a>


  </pre>
</blockquote>
When you use "for c2 in gen()]",  at each loop of c1, a new gen is
instanciated and looped-over to continue building the list.<br>
Whereas when you use "for c2 in it2)", it's always the same instance of
gen which is used (it2), so after 3 loops on c2 this instance is
exhausted, and the following iterations on c1 (and then attempts of
looping on c2) don't give anything because the looping on c2 gives an
exception "StopIteration".<br>
<br>
Regards, <br>
pascal<br>
<br>
<br>
</body>
</html>