delay and force in Python

Nick Coghlan ncoghlan at iinet.net.au
Tue Jan 25 08:53:26 EST 2005


Peter Otten wrote:
> 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.

Indeed. The main cases I am thinking of involve picking off the first few items 
of an iterator (either to use them, or to throw them away before using the rest).

And if an app actually *needs* random access, there's a reason lists still exist ;)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list