delay and force in Python

Peter Otten __peter__ at web.de
Mon Jan 24 07:41:52 EST 2005


Nick Coghlan wrote:

>> Py> print islice((x for x in xrange(1, 996) if x % 2 == 0), 1, 2).next()
>> 4
> 
> Wouldn't it be nice if this could be spelt:
> 
> print (x for x in xrange(1, 996) if x % 2 == 0)[2]
> 
> Well, I just put a patch on SF to enable exactly that:
> http://www.python.org/sf/1108272
 
I like it. Of course you always have to bear in mind that one giant leap for
a list could be _many_ small steps for an iterator.

Peter




More information about the Python-list mailing list