[ x for x in xrange(10) when p(x) ]
Alex Martelli
aleax at mail.comcast.net
Wed Nov 9 22:53:21 EST 2005
bonono at gmail.com <bonono at gmail.com> wrote:
> Alex Martelli wrote:
> > This becomes a valid list comprehension by writing 'if' instead of
> > 'when'.
> valid, yes. efficient, I am not sure.
>
> [ x for x in xrange(10000000) if p(x) ]
>
> means I need to go through the whole range even if p = lambda x: x < 2.
Of course, barring semantically-very-deep optimizations (ones no
production implementation I know of ANY language would even try). And,
your POINT would be...?
Alex
More information about the Python-list
mailing list