A python boolean function can easily be negated with lambda functions, but it's a bit verbose and hard to read for something so basic, for example:
def is_even(n):
return n % 2 == 0
odds_under_50 = filter(lambda x: not is_even(x), range(50))
I'm wondering if there is a function to do this in the standard library, which might look like:
odds_under_50 = filter(negate(is_even), range(50))
[xi for xi in range(50) if not is_even(xi)]filterand this list comprehension are not equivalent sincefilteris lazy: you canfilteran infinite generator whereas you cannot do that with list comprehension.import itertoolsand then do[xi for xi in itertools.repeat(1) if not is_even(xi)]this will run out of memory.filterwill evaluate lazily and thus not consume CPU/memory at all.