itertools.filterfalse - what is it good for

Discussion in 'Python' started by Wolfgang Maier, Mar 8, 2013.

  1. Dear all,
    can anybody point out a situation where you really need itertools.filterfalse() ?
    So far, I couldn't think of a case where you couldn't replace it with a
    generator expression/if combination.
    e.g.,

    a=filterfalse(lambda x: x%2, range(1,101))
    b=(i for i in range(1,101) if not i % 2)

    do not return the same object type, but otherwise are achieving the same thing.
    What am I missing here? For sure filterfalse exists for a reason?

    Best,
    Wolfgang
    Wolfgang Maier, Mar 8, 2013
    #1
    1. Advertising

  2. Wolfgang Maier

    Neil Cerutti Guest

    On 2013-03-08, Wolfgang Maier <-freiburg.de> wrote:
    > Dear all,
    > can anybody point out a situation where you really need
    > itertools.filterfalse() ? So far, I couldn't think of a case
    > where you couldn't replace it with a generator expression/if
    > combination. e.g.,
    >
    > a=filterfalse(lambda x: x%2, range(1,101))
    > b=(i for i in range(1,101) if not i % 2)
    >
    > do not return the same object type, but otherwise are achieving
    > the same thing. What am I missing here? For sure filterfalse
    > exists for a reason?


    It must exist for reasons of convenience and efficiency only.

    It can trivially be replaced by filter in all cases (at least in
    Python 3), but it saves you from a possibly slow extra function
    indirection, and also from needing to define one at all.

    --
    Neil Cerutti
    Neil Cerutti, Mar 8, 2013
    #2
    1. Advertising

  3. Wolfgang Maier

    Miki Tebeka Guest


    > 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)
    Miki Tebeka, Mar 9, 2013
    #3
  4. Wolfgang Maier

    Miki Tebeka Guest


    > 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)
    Miki Tebeka, Mar 9, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    862
  2. Steven Bethard
    Replies:
    0
    Views:
    385
    Steven Bethard
    Mar 12, 2005
  3. Raymond Hettinger
    Replies:
    17
    Views:
    534
    Simon Brunning
    Feb 18, 2008
  4. Nick Mellor
    Replies:
    35
    Views:
    340
    Paul Rubin
    Dec 6, 2012
  5. Terry Reedy
    Replies:
    0
    Views:
    108
    Terry Reedy
    Mar 9, 2013
Loading...

Share This Page