 Welcome back, everyone. In this video, we're going to cover the filter function. And this is very similar to the map function we covered in the previous video, where we have some sort of function. I actually put this as fun, because I think filters are fun. And some sort of iterable containers. It's going to return true if it matches the filter. This is important. If it matches the filter, it will be placed in the result. Otherwise, it will not. This is a really powerful way of filtering things. We're going to look at two example usages of the filter function. The first one is just simply getting a sub range. And when I say simply, this is actually pretty powerful. So I'm going to import random. We've talked about random before. And then I'm going to say some values, which is going to make a blank list of values for x in range. And we're going to make 10 items. And in those 10 items, we want to get a random number. So I'm going to say v dot append, and we'll say random dot, random range, this is going to take a range from one, I should say zero all the way up to whatever. There we go. So we're going to say random range. And then we're just going to print this out. Just so we can see what our range actually returned. Let's go ahead and run this. So we're just getting some random numbers 96. I mean, you see they're just all over the place. If I run this multiple times, you see the numbers are changing down there. So what we're going to do now is say I want to know the lower half of this boss walks in the room and says I don't want anything over 50. Okay, so I'm going to make a little function. We're going to take our value. I'm going to say if value is less than 50, then I want to do something with it. I want to return through because the boss said he only wants to see values less than 50. Otherwise, I'm going to say else. And we're going to return else. This is about as simple as it gets. This is basic filtering. That's really what a filter does. Does it match yes or no? That's it. That's really as complex as it needs to be. So I'm going to say f equals and we're going to go ahead and filter using the lower function. Remember, we had this conversation in the last video. So we have our function call, and then we have our values. This list of values right here. Each one of these is going to get called in turn. So it's going to call it on 57 72 dot it on so on. And the logic here is it's going to say, is that individual value like 57? Is it less than 50? False 70. False two true. I think you understand the logic here. So let's go ahead and print this out and say print f and I want to say less than I help if I put that in the actual quotes there. Less than 50. When your keyboard betrays you. Okay, so we're going to convert that into a list. Otherwise, we're just going to get back a function object which really doesn't help us out a whole lot. So here's our number set and the items that are less than 50 1733 45 and 11. So this is what I mean by it is extremely fun and powerful. Very quickly, can you just take some sort of data set here and just say you know what, I only want to see certain items and then build some sort of custom logic to get a sub range of that data. Now, even though this video series is aimed at the complete newbie I don't like to stay in newbie land very long so we're going to jump into a little bit more of an advanced topic and we're going to actually filter types. So when I say filter types, we're talking about classes. So we're going to filter an animal. And we're just going to make a class real quick here. I'm going to say name, name error, my keyboard has betrayed me. And we're going to say, instructor here. Bear with me super quick as I make some little plumbing code. Get our constructor up and roll in here, self dot name. What's the name? All right, now that we've got our animal class and I'm not going to make this super complex. We can just take this copy, paste, let's call this hat. It's going to inherit animal. And because we're inheriting we can get rid of this and we can get rid of this. And it's super simple from here. We just say super. Get it super simple. Anyways, I won't quit my day job. We're going to go ahead and initialize it with the name. I could actually spell name. There we go. Because I know there's also dog lovers out there. We're going to make a dog class as well. And anybody who knows dogs and cats know they typically don't mix well together, although I've been pretty fortunate in life where every dog and cat combination that I've had they've just really gotten along together really well. So animal, we have a cat, which is an animal dog, which is an animal, but these are two different classes that don't even know the other exists. So this is where the complexity comes in and you're going to see this time and time again. What have we just done? Well, we're making a list that will be called animals and we're going to dump cats and dogs into the same list. So I'm going to say for x in range. And we want from 10, I should say zero to nine, but we're going to go ahead and let's say if, and then we're going to do something a little funky here, we're going to say x mod to zero. Basically what we're doing here is we're saying we only want the even number. Then we're going to go ahead and say animals dot append. We're going to make a new cat. And we're going to give it some type of name. So let's go back up here real quick. And I'm going to say name equals animal, plus whatever the number is. All right. So once we've gotten to this point, then if it's not even, it is. Well, you guessed it odd. And we can just simply grab this. And maybe I should have done that backwards because cats are just odd dogs are usually pretty even, but cats are usually really weird. All right. So anyways, we've got that. Now we're going to go ahead and print out our animals, just so we can see that we have a mixture of them and sure enough, a bit of gobbly guck on the screen. But we have cat dog, cat dog, cat dog, cat dog. So very simple, very easy to understand what we've just done. We've made a list that contains both cats and dogs. And it's just a jumbled mass. Now I want to sort this out a little bit, say for a in animals. And I'm just going to print this out make it look nice and pretty on the screen. And I'm just having a good day. Enjoying my coffee. And we'll say animal. And super smart of me to do this. Now it looks super great. And I'm going to impress the boss. And I'm going to get a raise and oh, name. Yes, because we hard coded it. Very easy to fix this. Yes, my boss is walking around the corner. I'm going to show him this. I'm going to save the company millions of dollars. And sure enough, I have my nice little collection of animals here. And the boss says that's great. But I only want cats. And then I only want dogs. I want two different lists, but you have to maintain this single list. Now I got to write all this code. Well, this is where the filter function comes in as you might have guessed. So I'm going to say dev cats. And we're going to make a cat filter. It just sounds really, really cool, a cat filter. So we're going to turn is instance. And I'm going to say is that value an instance of the cat class. That's it. That's really as complex as we need to make this we just want to make sure it is an instance. We can do the same thing for dogs. And then we can use that filter function to utilize these two functions to their fullest extent. So I'm going to say or see in. And then I want to make a list. And we're going to filter. Because remember filters going to return a filter object, need to convert that to a less we're going to filter the cats using the animals, slow and way down so it makes sense here. We're going to print. Actually, I'm going to say cats. And I'm going to explain this in case you're completely lost, don't worry. All right, so what we're doing is very similar to our for a an animals, except for we're saying for C in and we're going to make a list from whatever filter gives us and we're telling filter, use the cats function. And use the animals collection. Remember animals is a mixture of cats and dogs. And we're just going to say for each item determine if it is a cat, we're just going to return is instance. And then we can do the same thing for dogs. Very simple, very easy to understand. And when we run it now we have beautifully separated these. Now we have cats. And those are the evens. And then we have dogs. And those are the odds. Okay, pretty much sums it up for this video. Main takeaway from this is filters going to return a filter object we need to pass a function and some sort of iterable container. From there it just really needs to return a true or a false if it's true it's going to end up in the result list. I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help myself and thousands of other developers are hanging out in the void realms Facebook group. This is a large group with lots of developers and we talk about everything technology related not just the technology that you just watched. And if you want official training I do develop courses out on udemy.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there. Drop me a note I'm either working on it or I will actually develop it. I will put a link down below for all three of those and as always help me help you smash that like and subscribe button. The more popular these videos become the more I'll create and publish out on YouTube. Thank you for watching.