What's the most idiomatic way of writing a filter
with a negation?
Example:
is_even= lambda x : x % 2 == 0
odd_numbers= filter( lambda x: not is_even(x), range(10) )
Of course, you can just use list comprehensions - but then you needn't use filter
anyway
In case anyone wonders, I've stumbled upon this while trying to split a list based on a condition
)
a typo? – Midgutodd = filter(lambda x: x % 2, range(10))
? – Transferenceitertools
module has ifilterfalse(), which filters for elements where the function returns False. (itertools.filterfalse() in Python 3). No built-in equivalent, butnot
seems simple enough. – Organologyis_even
is already defined – Catilineifilterfalse
seems exactly what I was looking for, and exists for python 2. If you care to submit an answer, I'll accept it – Catiline