Python 2.0b1 List comprehensions are slow

Robin Becker robin at jessikat.fsnet.co.uk
Sat Sep 9 04:55:39 EDT 2000


In article <14776.60842.586901.705258 at beluga.mojam.com>, Skip Montanaro
<skip at mojam.com> writes
...
>
>Seriously, inputs on how to improve the generated code are welcome, though
>I'm skeptical there's much that can be done (the list's append method is
>already cached as I recall, which is where the speedup over for loops comes
>from), certainly before 2.0final.  If a significant speedup was possible
>without a major reworking of the Python VM, I think we'd have seen it by now
>in the bytecode generated for for loops.
>
seems to me that this kind of code could be optimised by using the map
idiom. To me
        [expr for x in L] seems to correspond to 
        
        map(lambda vars: expr,L)

with an if condition where you would use 
        [expr(x) for x in L if cond(x)
        
        map(lambda x: expr,filter(lambda x: cond,L))

and the mapping 'seems' relative straight forward. I wonder why we don't
have mapfilter and filtermap functions? They would seem quite easy to
implement.
-- 
Robin Becker



More information about the Python-list mailing list