[ x for x in xrange(10) when p(x) ]

bonono at gmail.com bonono at gmail.com
Wed Nov 9 22:47:47 EST 2005


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.




More information about the Python-list mailing list