Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Filter Iterables With filterfalse()

00:00 In this lesson, you’ll learn what the filterfalse() function is and how to use it. So far, you’ve used the filter() function to extract values from an iterable that satisfy a certain condition, but what if you want to do the opposite and extract values that don’t satisfy that condition? That’s where filterfalse() comes in handy.

00:22 With filterfalse(), you can quickly and intuitively keep the values that evaluate to False and filter out the ones that evaluate to True. filterfalse() is, as you might have guessed, the inverse of filter().

00:37 filterfalse() is part of the itertools module, so unlike filter() that is readily available without importing anything, you’ll have to import filterfalse() from itertools to be able to use it.

00:50 The filterfalse() function is also useful to promote code reuse. How? you might ask. Well, it promotes code reuse by providing a reusable tool for filtering out elements based on a given predicate function.

01:04 Instead of writing custom code to filter out elements that don’t meet a certain criteria each time it’s needed, you can just reuse a filterfalse() function to achieve the same result with less code.

01:18 The filterfalse() function takes in two parameters: function and iterable. function provides the criteria to keep the values that evaluate to False, and iterable can be any Python iterable, such as lists, tuples, sets, and iterable objects, such as generators.

01:39 Now that you have a good understanding of how filterfalse() works, let’s explore an example of how to use it. Extract odd numbers using filterfalse().

01:49 Your goal here is to use the is_even() function from the extract even numbers example, but this time extract odd numbers instead of even ones.

02:01 First of all, let’s import filterfalse() from itertools. from itertools import filterfalse.

02:12 Now let’s re-create the same list of numbers from the extract even numbers example. numbers = [1, 3, 10, 45, 6, 50]. Also, let’s re-create the same filtering function from the same example.

02:32 As a reminder, it checks whether a number is even or not by checking its remainder when you divide it by 2. To do that, it uses the %. def is_even(), number as an input.

02:48 return number % 2 == 0. Let’s try it out with 3. So is_even(3). It will return 3 % 2 == 0. 3 divided by 2 has a remainder of 1, so 1 is not equal to 0, and is_even() will return False.

03:13 Up until now, this is the exact same solution as before. Now comes the interesting part. Let’s use filterfalse(). And of course, here, just like filter(), you’ll have to call the list() function on the result to be able to print it on the console. list(filterfalse()), is_even as the predicate function, and numbers as the iterable argument. Perfect.

03:42 Now the result you’re expecting is the odd numbers here, so 1, 3, and 45. Let’s see if it works. And there you go. You got a list of numbers: 1, 3, and 45.

03:57 Let’s think about what happened here. filterfalse() applied is_even() to every number in numbers. If is_even() returned False, filterfalse() kept it, and if it was True, it got rid of it. Exactly the opposite of filter().

Become a Member to join the conversation.