Filter5:09 with Kenneth Love
Filters are a very useful utility in programming, letting you refine a group of items into just the ones that meet your criteria. It's a "must be this tall to ride" sign for your data!
filter() takes a function and an iterable. The function, like with
map(), takes only a single argument and is applied to each item in the iterable. If the function returns a truthy value for the item, the item is sent back to
filter() which, ultimately, returns a new iterable of the filtered items.
You can achieve the same effect with
[item for item in iterable if func(item)]. Again, like with
map(), this can be more quickly readable for small applications.
filterfalse(). This function works just like
filter() but only returns things where the filter function gives back a
False or non-truthy value. You can read more in the documentation.
Watch our comprehensions workshop if you want more information.
I'll be honest, out of all the things that we have and 0:00 are going to talk about in this course filter is probably the one I use the most. 0:03 Filter, like map, takes an iterable and a function and 0:08 runs that function against each item in the iterable. 0:11 If the function returns a truthy value for 0:14 the item then that item gets added to the iterable that filter returns. 0:16 While it's quite a simple concept, I think you'll find that it's very handy. 0:19 So filter is one of the things that I probably use the most out of 0:24 all of Functional Programming. 0:28 I find it really super handy. 0:30 So, let's talk about it. 0:32 I mean I already gave you a bit of an introduction of it, so 0:33 I don't wanna talk about it too much, but the idea is we have an iterable, 0:37 we have a function, we run that function on each item in the iterable, and 0:41 if that function returns true for that item, then we keep that item around and 0:45 we throw away the ones where that function returns false. 0:51 So that means, we have to write a function again. 0:54 So let's write this new function here, we'll call it is_long_book and 0:57 it's gonna take a book. 1:01 Often you'll find that map functions would end up being something like 1:03 apply sales price, and filter functions start with like 1:09 is or are or something like that, where you're kind of like asking a question. 1:15 This naming convention just kind of helps with like oh yeah, 1:19 I'm filtering this to where it is a long book and then I'm gonna do whatever. 1:21 That's kind of the idea. 1:25 So let's just say here Does a book have 600 or more pages? 1:26 That's how we're defining a long book, we're saying, hey 600 pages. 1:35 So return book.number_of_pages >= 600. 1:38 So if a book has 600 pages or more, then we're gonna return true. 1:43 If a book has 599 pages or less, we're gonna return false. 1:50 That's all we're doing. 1:54 Filters are not always super simple functions, 1:55 you might have a complex function, but they always end up returning just, true or 1:59 false, or they return an object. 2:03 They return something truthy. 2:05 So, let's say long_books = 2:06 filter(is_long_book, BOOKS), 2:10 and just like with map, if we wanna be able to get to each of these items, 2:16 we need to put this into a list. 2:19 So, let's print len books, and 2:22 let's print len long books. 2:27 All right. 2:31 So there are 28 books total, and 12 of them are long books. 2:34 Stephen King definitely likes to write a lot of books, 2:37 or he likes to write a lot of words and thus a lot of pages in his books. 2:42 So what we've done, just to recap here, we're running a filter. 2:46 So is_long_book takes each item that's in BOOKS and runs this filter over it. 2:49 So pretty simple, pretty straightforward. 2:56 Again, this might be one where you're like, hey, 2:59 that looks an awful lot like something you did in that comprehensions workshop. 3:02 And I'd be like, yep, you caught me, it is. 3:06 So let's see how to do this as a comprehension. 3:10 So we'll say long_books2, and 3:12 we'll say book for book in BOOKS if, and 3:16 then here we could say is_long_book(book), 3:21 or we can just do if book.number_of_pages is >= 600. 3:27 So we can eliminate the use of this function all together. 3:34 And if we print long_books2 then we still get those same numbers, 3:38 so list comprehensions we get kind of two ideas. 3:44 We can apply a function at the beginning, and that gives us the map, or 3:49 we can do an if condition at the end, and that gives us a filter. 3:53 You can of course combine those, and we're gonna talk about that in a minute. 3:56 So, the idea though is you can do these two different ways. 4:00 Just like with map, which do you pick, do you pick filter, or 4:04 do you pick a list comprehension? 4:07 Again it really kinda comes down to, how hardcore this code going to end up being. 4:09 Is it going to be that I'm filtering this down one time? 4:15 Sure, use that list comp especially if it's a simple easy filter. 4:18 Is it gonna be hey, I've got to be able to call the same 4:22 kind of filter on a whole bunch of different things? 4:25 Or I need to be able to use this filter at a bunch of random points, or 4:28 is it that I need to be able to filter down this large list of items, 4:31 then I also need to be able to filter individual small items occasionally, 4:34 then filter and the filter function probably make more sense. 4:39 So it kinda becomes one of those things where you just learn it as you go. 4:42 If you want to get back an iterable of items that are falsey for 4:46 the function, you can change the inside of your filter function so 4:49 that it returns false instead of true for positive matches. 4:52 This can be kind of confusing logic though, so in the Itertools module 4:55 there's another function named filter false that works just like filter, but 4:59 only captures the items that return false. 5:03 I'll put a link to the docs for 5:05 filter false in the teachers notes in case you need it 5:07
You need to sign up for Treehouse in order to download course files.Sign up