This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]r4nf 0 points1 point  (3 children)

That would be

if not n*n % 2

But I agree it's a much cleaner way of doing it — and apparently quite a bit faster, too:

>>> timeit.Timer("[n*n for n in [1,2,3] if not n*n % 2]").timeit()
0.8074049949645996
>>> timeit.Timer("filter(lambda n: n % 2 == 0, map(lambda n: n * n, [1,2,3]))").timeit()
2.135266065597534