i=2; lst=[i**=2 while i<1000]

Peter Otten __peter__ at web.de
Tue Dec 6 12:18:01 EST 2005


bonono at gmail.com wrote:

>>>>> list(iter(lambda b=[2]:b.append(b[0]**2) or b[0]<1000 and b.pop(0) or
>>None, None)) [2, 4, 16, 256]
>> 
> out of curiosity, what stops the iterator ?

"""
Help on built-in function iter in module __builtin__:

iter(...)
    iter(collection) -> iterator
    iter(callable, sentinel) -> iterator

    Get an iterator from an object.  In the first form, the argument must
    supply its own iterator, or be a sequence.
    In the second form, the callable is called until it returns the
sentinel.
"""

The lambda is used as the callable, None as the sentinel.

Peter



More information about the Python-list mailing list