Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Data python 3
  • Overview
  • Transcript

2.5 Filter

Similar to map, filter is a tool for efficiently handling collections of data. In this lesson, I'll show you how you can replace some kinds of data-picking loops more concisely with a filter.

2.5 Filter

In a previous lesson, when we started to talk about the map function, we learned that we can start to take collections of data, and apply some predefined function to the values within that collection, and get some sort of result back. In that lesson, I started to allude to the fact, that we're starting to lean towards the world of functional programming, where, we're getting away from some of these control structures. We're getting away from things like for loops and conditionals and using functions in those instead. Now, that's all fine and dandy, but what are some other ways of doing that? And what if I needed to adopt or adapt some of the values that are in a collection and I only wanna pick and choose a few different values that are within their particular collection to use my function. How can I do that? Well, we started to learn that by using map we can apply a function to a number of values but how can I trim some of those values? So let's take a look at an option we have for that. So in this case, I'm going to start with another predefined function. And we're going to use sweet again, because this is another sweet function. In this case we have sweet and it's gonna take in a value X. And it's gonna return some sort of value. So in this case, it's going to return x and we're gonna add to it the absolute value of x and then after that we are going to square it. We're gonna raise it to the second power maybe just to be a little different this time we'll it or raise it to the third power source went and save that. So in the math lesson we learned we can apply this function to a range of data. So let's do that, let's say that I want to print out the list that is generated from the result of a math function where we're gonna pass in suite and we're gonna give it a range this time. We're gonna give a slightly different range this time. We're gonna say from -4 up to 5 and when you provide range with two values like this, it's giving you the minimum and the maximum, but the minimum in this case is inclusive, the upper end is exclusive. So, it's gonna go from -4 up to 4. So, let's go ahead and save that. Now, let's see what the result of this is going to be, if I run Python. And we're gonna run it against our filter and as you see here, we're going to get a list result of pretty much what we would expect. The bunch of zeroes and then 8, 64, 216 and 512. Now, as you can see this map function is useful, we're able to apply a predefined user defined function against this collection of data. But, if you look at the result set here, the first values which all represent either zero or negative numbers, all result in the same output of zero. So maybe, it doesn't make much sense for us to waste cycles of our CPU to process values that are input that are zero or below. So maybe that's something that doesn't make much sense for us to do, so maybe we should get rid of that. So how can we stop processing those values and actually skip over those and just get to the meat of what we wanna do? Well, the way that we can do that is you can probably tell by the name of this file is used the filter function. Now, this is another one similar to map is it's going to take in a function and it's gonna take in a collection of data. It's going to filter out, so it's basically going to get rid of all of the things, and actually maybe a better way to think of it, is it's going to keep all of the values within the collection that meet a certain criteria. So how would we do that? Let's go ahead and start by creating another function. So, let's say I want to create a function that is going to take in a value, and then it's going to return true or false based on some condition that I've defined. So let's create a function, and we're gonna call this, positive. It's gonna take in value, x. And it's simply going to return whether or not x is greater than 0. So as you can see if I pass in a negative value it's going to return false. If I pass in a 0 it's going to return false. But if I pass a one or greater, it's going to return true. So now what I could do is I can use a filter function, so, we'll go ahead and print. Once again we're gonna print out a list here and we're gonna print out the result of the filter function. Then, we're gonna pass in positive and then we're gonna pass and once again that same range. And we'll adjust this a little bit as soon as we get to a point where you've seen this work. So let's go ahead and save that. So now let's flip back over. We'll go ahead and run our file again. And as you can see here, now my filter function is returning only the values that returned a positive or a true result from my function that I passed it, in this case positive. So what I would like to do is I would like to use my filter function along with positive to filter out the stuff that's being passed in this range that doesn't meet that criteria so that I'm not not getting a bunch of zeroes in there. I can get rid of all that stuff and only work with the good values. So let's combine these two to do that result. So I'm gonna print out the list of the result of my map function passing a range or a collection into suite. But I wanna modify this range a little bit. So I wanna take advantage of using filter with positive. And I wanna pass into positive the values from this collection or range. So let's go ahead make sure all of my parenthesis match up. So there we go, go and save that. And actually now I'm going to get rid of this print. We'll go ahead and save that so now, I'm going to filter my range first to get rid of all the negatives and zero. And then run the result of that through my map function with suite and then print out the list. So we'll switch back over. We'll run our application. And now we've gotten rid of all those zeroes at the beginning because those are always going to be the same values. And we've gotten just to the meat of what I wanted to get to. So as now you can see, you can use the filter built in function to be able to do these operations or to get rid of certain values that you don't want and only keep the ones that you want based on a certain predefined function.

Back to the top