Generator oddity
opstad at batnet.com
opstad at batnet.com
Fri May 1 07:58:16 EDT 2009
I'm a little baffled by the inconsistency here. Anyone have any
explanations?
>>> def gen():
... yield 'a'
... yield 'b'
... yield 'c'
...
>>> [c1 + c2 for c1 in gen() for c2 in gen()]
['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
>>> list(c1 + c2 for c1 in gen() for c2 in gen())
['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
>>> it1 = gen()
>>> it2 = gen()
>>> list(c1 + c2 for c1 in it1 for c2 in it2)
['aa', 'ab', 'ac']
Why does this last list only have three elements instead of nine?
More information about the Python-list
mailing list