Python has this function called 'filter',
and filtering is really just a way to apply a function and
to remove data that you don't want, just like the name intends.
So if you wanted every second number, every even number,
just vowels, you could do that.
And you could apply a filter against some sort of data type.
So just like the 'map' function, it takes a function name ,and
then some sort of iterable, something that we can loop over,
and if you were to run something like this, you're going to
see that there is some sort of memory point allocated to it.
And so you can't actually store this in a variable because
it's just going to be a piece of memory.
So let's go ahead and create an example here where we just
want all the even numbers.
So 'def get_even_numbers()', and then it's going to take a 'num'
[no audio]
and this is not called 'get',
let's call it 'filter_even_numbers',
and we're going to give this a Docstring "Returns only even
numbers". And what we can do here, is we can return whatever
that 'num' is going to be,
modulus, '% 2 = 0'.
Now, the thing with the 'filter' function here, is if it returns
True, then it's going to keep the data, whatever this value is
going to be, but if it returns False, it's going to discard it.
We'll see this in action in just a little bit.
And with this modulus, what this is saying is, if there's
a number, let's say 5 divided by 2, is there a remainder?
And if there is no remainder, if that remainder is 0,
then return True.
And that's what this modulus, this '%' sign is, that's
called modulus, and so that really just gets the remainder.
So let's go ahead and run that.
And let's create a list of numbers in here.
So 'nums', we've got 1-2-3-4-5-6, all the way to whenever
we want to stop, so 14, I guess.
And now what we can do, is we can loop through this,
so we can say 'for num in filter',
it's going to take the name of the function, 'filter_even_numbers',
and then the 'nums' itself.
So the 'nums' is referring to the iterable, it's a list.
So we can loop through it,
it's called iterable.
And 'filter_even_numbers' is referring to this function without
the parentheses. It knows to run this function on its own internally
so we don't need to execute it right now.
And then we could simply print the number.
Now, what this is going to do is, it's going to put number
1 in here. Is 1 divided by 2,
does that have any sort of remainder?
What about 2?
2 divided by 2, does that have a remainder?
3 divide by 2, does that have a remainder?
4 divided by 2, and it'll just do that over and over and over again.
So let's go ahead, print this,
and now we have just even numbers.
2-4-6-8-10-12, and 14.
Now, what happens if we just run this on its own?
'filter(filter_even_numbers())',
with 'nums' in here. Again, we get a memory location just like
the 'map' function gave us.
We can't even store this as a variable.
So let's say 'evens =', again, it's just, it's saying that
this is a 'filter', and is pointing to a piece of memory,
and we can't do anything with that.
But just like with 'map', what we can do, is cast this to a
'list', and if we do that, there's some magic behind the scenes
here. It will automatically give us a list of all the numbers.
And so essentially, what we've done is we've created a function
called 'filter_even_numbers', and we gave it something to loop
through, and it looped through 1-2-3-4, all the way up to
13 and 14,
and it applied this logic to it every single time.
Now, if this logic came back True, we had a number.
If the logic came back False, it was stripped out.
Now, where is this useful?
Actually, even numbers or odd numbers it is quite useful.
I've seen that quite a bit.
You can also strip out characters that aren't a vowel or
they are a consonant,
or if you're dealing with a big website, you could even filter
through users that have a certain permission.
So you could get all these users, and then you can loop through each one,
you can check to see if they're an admin, and just get a list
of admin users, and just return User IDs,
you could do something like that as well.
So this gets to be very, very useful down the road.
Let's go ahead and give 'filter' a shot,
give 'map' a shot.
[no audio]