itertools.filterfalse - what is it good for

Miki Tebeka miki.tebeka at
Sat Mar 9 21:30:11 CET 2013

> can anybody point out a situation where you really need itertools.filterfalse() ?
Sometimes you get the predicate as a parameter to another function. This way if you want to filter out things you can easily do it. Other language (such as Clojure) have a "complement" function that removes the need of filterfalse.

For example (Python 3):
    def percent_spam(is_spam, documents):
        n_spam = sum(1 for _ in filter(is_spam, documents))
        n_ham = sum(1 for _ in filterfalse(is_spam, documents))
        return float(n_spam) / (n_ham + n_spam)

More information about the Python-list mailing list