Jose: Hi welcome back.
In this lecture, we're talking about filter.
Now that we've got our generators,
iterators and iterables out the way,
we can start with some fun builtin functions.
The filter function is a builtin function
in Python that you can call from any file or programme,
that's what builtin means
and it takes two arguments.
The first argument to the filter function
is another function
and it also takes an iterable.
So it goes something like this,
let's find a friends list
like Rolf, Jose, Randy, Anna and Mary
and I know I've missed one.
And now we're going to use
the filter function so that we only get the friends
that start with the letter R.
So, start with R,
is going to be filter, this is the function,
now it takes a function
first argument, arg 1, function that returns true or false.
So the first argument to this filter function
has to be another function that returns true or false
depending on what we want to filter.
So let me define a function up here.
Starts with R.
Return friend dot start with R.
Friend dot starts with R is going to be true
if this argument here starts with an R,
it's going to be false if it doesn't.
So, the first argument to the filter function
has to be this starts
with R function
and the second argument has to be an iterable,
we know what these are so friends is an iterable
'cause we can iterate over it, it's a list
and that's it, this is what the filter function looks like
and what it does is it only keeps
the elements for which this returns true.
So, the first time the filter function
runs through, it's going to give you Rolf,
Rolf going over to the friends,
friend starts with R it's going to be true
so Rolf will be kept by the filter function.
Then it will go again, and this time with Jose,
Jose starts with R is false,
so filter will remove it and ignore it
and so on for every friend here.
The interesting thing is that filter
actually returns a generator
so that it's a bit more efficient,
instead of copying your list
with only the elements you want to keep,
you have to call the next, start with R
element so that you get the next available element
which is this case is going to be Rolf.
And if you do that again, and again,
you'll get Randy
and finally you'll get this stop iteration exception
that we talked about earlier on,
is what signals the end of a sequence
or a generator or an iterator
when you reach the end of it, okay?
When you do a for loop,
what the for loop is doing in the background
is just calling next and as soon as you reach
this stop iteration, the for loop stops
and it doesn't give you this error
because the stop iteration has been designed
to mean that and the for loop is a construct
that uses stop iteration to know
when it's reached the end, okay?
Now of course, if you do list of start with R,
then it's going to give you a list of all the elements
in the generator, it's going to go
through each element and it's going to put
it in a new list.
Now if you do it again,
what do you think is going to happen?
Well, you'll get an empty list,
the list object here doesn't raise a stop iteration
but it sees that the stop iteration is raised
and it just doesn't give you anything
it just gives you an empty list
now because the generator has already been used
and it's remembers that it's reached the end
so it can't give you anything else.
This is why I explain generators, iterators and iterables
before going into this filter function, okay?
Now of course, instead of defining a function here
that takes a parameter
and returns of the parameter starts with R
we could just used a lamda.
This lamda function is exactly the same thing
so it takes one parameter
and after the colon we define what it returns
and what it returns is x dot starts with R
so this is either true or false
depending on whether x starts with R.
If it makes you feel better,
you can call the parameter friend
and that way maybe it's a bit clearer
that maybe this function is going to operate
on these friends here,
then you no longer need that function there.
And we also no longer need this comment
since we know what it means now.
This filter function here
is identical
to this generator expression here,
this generator comprehension that says,
F for F in friends, if F dot starts with R.
So these two are identical, okay?
Here we've got ourselves a generator expression,
a generator comprehension that puts each friend
into a new generator but only if F starts with R,
so these two are the same, okay?
Which one is faster?
Which one performs better?
This one, generator comprehension performs better
in this case, at least.
And the reason for that
is because you have to create this lamda function
in this filter and you don't have to
create a lamda function in the generator expression.
If you already have your function defined elsewhere
in your programme,
the filter function can perform better
and also in some cases, I think
the filter function can be more readable
although in many other cases,
I think the list comprehension is more readable.
So it's up to you which one you use,
I normally go for the comprehensions,
it's also a bit more Pythonic.
And by the way, just for completeness
so it's clear exactly what's going on,
these two filter and this generator comprehension
is identical to this other function
that I'm going to define.
It's another filter function,
very much like this one
that takes in a function and an iterable
and is going to iterate over the iterable,
it's going to check whether the function
returns true or not
and it's going to yield I at the end
if the function is true.
So, let's imagine that we call my custom filter here.
Lamda friend starts with R is this function,
the iterable is our friends list.
We iterate over the iterable, we get the first element
that's Rolf.
If func I that is, friend starts with R
is true, which it is,
then we're gonna yield I, that's Rolf.
When we call next on this function,
we're gonna get Rolf and then we're gonna stop
right after line nine.
When we call next again, we're gonna go back
to the top, that's I,
we're gonna, that's Jose by the way,
the string Jose,
we're gonna check if the function matches,
it's gonna be false so we're not gonna yield anything,
we don't stop, we go over to the next element of the loop,
we check if Randy starts with R
and it is, it does, so then we yield Randy and we stop.
Next time we call next, we go over here
and it's Anna, it's false, so we don't yield,
we continue, I is Mary, it's false so we don't yield,
we continue, we reach the end,
that's a stop iteration so we reach the end of the
generator object and that's another stop iteration
which tells us that we've reached the end.
So hopefully this makes sense,
it is starting to get a bit more,
more into what Python is all about
which is this managing of data and sending data
from one place to another
and yield is extremely powerful
because it lets you stop and generate the values
one by one without having to store
them all in memory at the same time.
Anyway that's it for this video,
I just wanted to show you this filter function
and how it's used.
The filter function is also present
in many other programing languages
so you'll see it everywhere
as you develop your programming skills and you
continue your programming journey.
And so, yeah, that's it
and I'll see you in the next video.