Proposal: [... for ... while cond(x)]

Duncan Booth duncan.booth at invalid.invalid
Mon Aug 7 03:19:08 EDT 2006


Diez B. Roggisch wrote:

>> No, the list comprehension lets you write an expression directly
>> avoiding a function call, and it also allows you to add in a
>> condition which can be used to filer the sequence. Your proposal adds
>> nothing. 
> 
> It does. Consider this:
> 
> whatever = [x for x in xrange(1000000000) while  x < 10]
> 
> 
> That would run only in a splitsecond of what the whole listcomp would.

Except that the comparable listcomp today is:

whatever = [x for x in takewhile(lambda x: x < 10, xrange(1000000000))]

which also runs in a split second.

Actually, the OP was correct, it does add something: it removes the need 
for a function or lambda in the takewhile just as the original listcomp 
removes a function or lambda compared with the map version.



More information about the Python-list mailing list