Why does itertools filter on false?

This is mostly a naming/style/consistency question.

One observes that dropwhile drops the first element for which the function given returns True.

Yet filterfalse returns the iterable with the elements for which the function returns False.

The logic of these two is exactly opposite.

Now, of course, one can invert the logic of the function to get the opposite results, but it seems to me that having a method called filtertrue that can have inverted logic to return what filterfalse does seems rather more natural than where we are now.

I was wondering if anyone knew the history here or if there are obvious use cases that drove this choice.

Remember that the built-in function filter keeps items for which the predicate function returns True, so it makes sense there’s a filterfalse to do the opposite.

2 Likes

Oh, that makes sense. Thanks!