Hello guys.
So we are going to continue the discussion with respect to Python.
And in this video we are going to discuss about filter function.
Now what exactly is filter function.
So filter function constructs an iterator from elements of an iterable for which a function returns
true.
It is used to filter out items from a list based on a condition.
So finally, just to understand what this filter function do, it is used to filter out items from a
list or any iterable based on a condition.
Okay, so in order to make you understand, let me just go ahead and quickly, uh, write a code for
you.
So here I'm going to create a definition.
Let's say this is my function which will tell whether a number is, uh, even or odd.
Right.
Or let's say this particular number is just, uh, this function is just going to check whether the
number is even and it is just going to return true or false.
So here if I'm giving num okay.
And let's say that I'm going to write my condition.
If num percentile two is double equal to zero, then what we are going to do we are just going to return
true.
Right?
So perfect.
This works absolutely fine.
We have seen this kind of function a lot.
So now if I just go ahead and give my 24.
So it is just going to give me true because this is obviously a true number.
But let's say that now I have a list of elements.
And here I have like one, two, three, four, five, six, seven, eight, nine, ten, something
like this.
1112 okay.
Now I want to apply the same operation on top of this particular element.
And I want to just filter out which satisfies only this specific condition.
Right.
So now focus on the last statement.
It is used to filter out items from a list based on a condition.
Right.
So this is my condition and I want to filter out from this specific list, right?
So how do I do it?
Yes, I can actually do it.
I can just iterate through all this particular elements inside this even function.
And then I should be able to do it right.
I should be able to get true, you know, probably append it in another list and get the values.
But let's do this with the help of filter.
Okay.
Now in filter if if you know about filter right filter usually takes first parameter as again function.
So in this case even is my function.
And the second parameter will be my list okay.
Now what this filter does is that whatever condition I have written inside this particular function
only that will get applied into this and the elements will be taken from the list only, which satisfies
this condition.
Right?
Which is returning true only that elements will be taken out.
Okay, so here when I say filter even of list.
And let me just go ahead and probably convert this into a list, you will be able to see that I will
be getting all the elements that are only even.
Right.
So this is a basic example to understand about filter function, right?
So it applies a kind of filter on the list that we specifically have.
Okay A very easy, uh, with respect to this.
Now let's go ahead and see more examples.
And this time I'm going to take filter with a lambda function.
Okay.
So lambda function and I will try to use the same thing.
Or let me just use some other things like let's say this is my number variable.
I have like 123456789 okay.
And then I will say hey do a filter and try to just take out greater than five.
Okay.
Greater than five okay.
Try to create a filter, something like that.
From this particular list I should only get numbers that are greater than five.
So now I will just go ahead and write list.
I will go ahead and use filter function.
Now let me just go ahead and define my lambda function.
Write Lambda function will be very simple.
If I go ahead and write just lambda expression this will this will be my argument.
Oh, Uh, sorry.
Uh, yes.
Argument.
And I'll be writing my expression.
That is, x is greater than five and on which list I will basically get, uh, apply this entire thing
in my numbers.
Right.
So this entire function is going to get applied on this particular list.
Now, once I go ahead and print it, you will be able to see that I will only be getting the elements
greater than five.
Right.
So this is one amazing thing with respect to list.
And uh, here with a simple just logic, how you are able to use filter.
Even in the previous video we saw about Lambda also.
Now I will show you filter operation with uh with multiple condition.
Okay.
So here I'm going to say filter with a lambda function, lambda function and multiple conditions.
So just to make you understand what all things are possible okay.
So multiple conditions now as usual I'll take this numbers okay.
Right.
And let's say I will write a variable even.
And it should be even I'm writing two check okay.
Even and greater than five okay.
So this is my variable that I'm actually going to create two things I'm actually doing.
Okay.
First of all I will check whether the number is even or not, will filter out based on the number is
even or not, and it should be greater than five.
So in this I will again use list.
I will again use filter.
Okay.
And along with that I will use my lambda x x colon greater than five okay.
So this is my one condition.
And I will go ahead and use one logical.
And so and operation over here I will use and this will be x percentile two or modulus two is double
equal to zero.
So these are my two conditions.
If it is getting satisfied.
And this will get applied to all my numbers list of numbers that is available over here.
So inside this lambda function what we are doing we are applying two expression.
One is x is greater than five and the other one is x modulus two uh is double equal to zero.
Right now if I go ahead and print.
So what it is going to do.
First of all it is going to check whether it is even or not and then whether it is greater than five.
So out of this elements, you will be able to see that I am able to get only six and eight remaining.
All elements are odd or less than five.
Perfect.
Uh, let's go ahead and see one more way of doing this.
Okay, now let's say that I want to apply a filter operation not only on list, let's say on dictionaries
okay.
So here I will just go ahead and do this okay.
I will say hey apply filter to check if the age is greater than 25 in dictionary.
Okay, so here you can basically see I will just create some data.
And again I have already created this kind of data.
So let me see from my which functions map function I think I have I've actually used this kind of data.
Uh, yeah.
So this is my people data.
Okay.
So here I have specifically people data.
I will use this same thing okay.
Just trying to save time.
Okay.
Now from this I will create a function where I'll say age greater than 25.
Okay.
And uh, let's say I'll just make some more data over here, I will cope.
I'll put over here.
I'll say Jack.
Then along with this, I'll say, John, age will be 25.
Okay.
And then what we are going to basically do, we are going to create a function here.
We are going to give the person information, one item person information.
And then we are just going to return the person of age, right?
And this age should be greater than 25.
And based on this, either it will return true or false.
Right now we are just going to apply this with the help of filter.
And we will use this particular function.
So I'll say filter.
And this will be my age greater than 25 along with my people because I need to apply this on my entire.
Um, uh, what to say collection that is iterable.
And then I'll try to convert this into a list.
So if I go ahead and print it.
So here you'll be able to see name age age 32 and 33, because this particular condition checks only
and returns only the age based on the age condition greater than 25, it will return those specific
records.
And yes, from this particular filter operation, you'll be able to see that I'm able to do this.
right.
So these are some of the amazing operations that you can do with filter.
So I would definitely like to put some kind of conclusions.
And I'm doing that particular thing with respect to every video that I, uh, record in this.
So here these are some of the conclusions.
So here you can see the filter function is a powerful tool for creating iterators that filter items
out of an iterable based on a function.
It is commonly used for data cleaning filter objects and removing unwanted elements.
By mastering filter, you can write more concise and efficient code for processing and manipulating
collections in Python.
So yes, this was about filter.
Now as we go ahead, we will be discussing about and again we will be reusing all these things.
But I think you got a clear idea about functions different types of functions.
Anonymous function Lambda function.
Map function.
Filter function.
Uh so yes, this was it for my side.
I will see you all in the next video.
Thank you.
Take care.