Movatterモバイル変換


[0]ホーム

URL:


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:00In this lesson,you’ll learn what thefilterfalse() function is and how to use it.So far, you’ve used thefilter() function to extract values from an iterable thatsatisfy a certain condition,but what if you want to do the opposite and extract values that don’t satisfythat condition? That’s wherefilterfalse() comes in handy.

00:22Withfilterfalse(),you can quickly and intuitively keep the values that evaluate toFalse andfilter out the ones that evaluate toTrue.filterfalse() is,as you might have guessed, the inverse offilter().

00:37filterfalse() is part of theitertools module,so unlikefilter() that is readily available without importing anything,you’ll have to importfilterfalse() fromitertools to be able to use it.

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

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

01:18Thefilterfalse() function takes in two parameters:function anditerable.function provides the criteria to keep the valuesthat evaluate toFalse,anditerable can be any Python iterable, such as lists,tuples, sets, and iterable objects, such as generators.

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

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

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

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

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

02:48return number % 2 == 0.Let’s try it out with3. Sois_even(3).It will return3 % 2 == 0.3 divided by2 has a remainder of1,so1 is not equal to0, andis_even() will returnFalse.

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

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

03:57Let’s think about what happened here.filterfalse()appliedis_even() to every number innumbers.Ifis_even() returnedFalse,filterfalse() kept it, and if it wasTrue,it got rid of it. Exactly the opposite offilter().

Become a Member to join the conversation.

Course Contents

Overview
66%

[8]ページ先頭

©2009-2026 Movatter.jp