WEBVTT

00:00.320 --> 00:00.980
Hello guys.

00:00.980 --> 00:03.470
So we are going to continue the discussion with respect to Python.

00:03.470 --> 00:06.890
And in this video we are going to discuss about filter function.

00:06.920 --> 00:08.780
Now what exactly is filter function.

00:08.780 --> 00:14.600
So filter function constructs an iterator from elements of an iterable for which a function returns

00:14.600 --> 00:15.170
true.

00:15.200 --> 00:19.280
It is used to filter out items from a list based on a condition.

00:19.280 --> 00:26.120
So finally, just to understand what this filter function do, it is used to filter out items from a

00:26.120 --> 00:29.300
list or any iterable based on a condition.

00:29.330 --> 00:35.090
Okay, so in order to make you understand, let me just go ahead and quickly, uh, write a code for

00:35.090 --> 00:35.420
you.

00:35.420 --> 00:37.790
So here I'm going to create a definition.

00:37.790 --> 00:43.730
Let's say this is my function which will tell whether a number is, uh, even or odd.

00:43.760 --> 00:44.000
Right.

00:44.030 --> 00:48.470
Or let's say this particular number is just this function is just going to check whether the number

00:48.470 --> 00:50.600
is even and it is just going to return true or false.

00:50.600 --> 00:52.880
So here if I am giving num okay.

00:52.910 --> 00:55.880
And let's say that I'm going to write my condition.

00:55.880 --> 01:00.740
If num percentile two is double equal to zero, then what we are going to do we are just going to return

01:00.760 --> 01:01.270
True.

01:01.300 --> 01:01.750
Right.

01:01.750 --> 01:02.680
So perfect.

01:02.680 --> 01:03.940
This works absolutely fine.

01:03.940 --> 01:05.770
We have seen this kind of function a lot.

01:05.770 --> 01:08.230
So now if I just go ahead and give my 24.

01:08.230 --> 01:12.070
So it is just going to give me true because this is obviously a true number.

01:12.460 --> 01:16.750
But let's say that now I have a list of elements.

01:16.750 --> 01:21.670
And here I have like 123456789 ten something like this.

01:21.880 --> 01:29.350
1112 okay, now I want to apply the same operation on top of this particular element.

01:29.350 --> 01:34.930
And I want to just filter out which satisfies only this specific condition.

01:34.960 --> 01:35.380
Right.

01:35.380 --> 01:37.540
So now focus on the last statement.

01:37.540 --> 01:41.620
It is used to filter out items from a list based on a condition.

01:41.650 --> 01:42.160
Right.

01:42.160 --> 01:46.090
So this is my condition and I want to filter out from this specific list.

01:46.120 --> 01:46.600
Right.

01:46.600 --> 01:47.770
So how do I do it.

01:47.800 --> 01:48.880
Yes, I can actually do it.

01:48.880 --> 01:52.270
I can just iterate through all this particular elements inside this even function.

01:52.270 --> 01:54.580
And then I should be able to do it right.

01:54.580 --> 01:59.170
I should be able to get true, you know, probably append it in another list and get the values.

01:59.170 --> 02:02.220
But let's do this with the help of filter.

02:02.280 --> 02:04.140
Okay, now in filter.

02:04.770 --> 02:07.110
If if you know about filter, right.

02:07.140 --> 02:10.470
Filter usually takes first parameter as again function.

02:10.470 --> 02:12.840
So in this case even is my function.

02:12.840 --> 02:15.510
And the second parameter will be my list okay.

02:16.440 --> 02:21.420
Now what this filter does is that whatever condition I have written inside this particular function

02:21.420 --> 02:27.300
only that will get applied into this and the elements will be taken from the list only which satisfies

02:27.300 --> 02:28.410
this condition.

02:28.440 --> 02:28.800
Right?

02:28.800 --> 02:32.010
Which is returning true, only that elements will be taken out.

02:32.040 --> 02:32.370
Okay.

02:32.400 --> 02:35.280
So here when I say filter even off list.

02:35.280 --> 02:41.130
And let me just go ahead and probably convert this into a list, you will be able to see that I will

02:41.160 --> 02:44.850
be getting all the elements that are only even right.

02:44.880 --> 02:50.160
So this is a basic example to understand about filter function, right.

02:50.160 --> 02:54.510
So it applies a kind of filter on the list that we specifically have.

02:54.540 --> 02:54.960
Okay.

02:54.990 --> 02:57.210
A very easy uh with respect to this.

02:57.210 --> 02:59.550
Now let's go ahead and see more examples.

02:59.550 --> 03:02.890
And this time I'm going to take filter with a lambda function.

03:03.040 --> 03:03.550
Okay.

03:03.550 --> 03:06.610
So lambda function and I will try to use the same thing.

03:06.610 --> 03:10.660
Or let me just use some other things like let's say this is my number variable.

03:10.660 --> 03:14.770
I have like 123456789 okay.

03:14.770 --> 03:21.850
And then I will say hey do a filter and try to just take out greater than five okay.

03:22.000 --> 03:24.010
Greater than five okay.

03:24.040 --> 03:25.840
Try to create a filter, something like that.

03:25.840 --> 03:28.960
From this particular list I should only get numbers that are greater than five.

03:28.960 --> 03:31.210
So now I will just go ahead and write list.

03:31.240 --> 03:33.610
I will go ahead and use filter function.

03:33.640 --> 03:35.800
And let me just go ahead and define my lambda function.

03:35.800 --> 03:37.870
Write Lambda function will be very simple.

03:37.870 --> 03:43.150
If I go ahead and write just lambda expression this will this will be my argument.

03:43.180 --> 03:43.780
Oh, sorry.

03:43.810 --> 03:44.620
Uh, yes.

03:44.650 --> 03:45.250
Argument.

03:45.250 --> 03:46.960
And I'll be writing my expression.

03:46.960 --> 03:53.800
That is x is greater than five and on which list I will basically apply this entire thing in my numbers.

03:53.830 --> 03:54.010
Right.

03:54.040 --> 03:57.580
So this entire function is going to get applied on this particular list.

03:57.610 --> 04:03.180
Now once I go ahead and print it you will you'll be able to see that I will only be getting the elements

04:03.210 --> 04:04.230
greater than five.

04:04.260 --> 04:04.620
Right.

04:04.650 --> 04:07.350
So this is one amazing thing with respect to list.

04:07.350 --> 04:12.420
And here with a simple just logic how you are able to use filter.

04:12.450 --> 04:15.480
Even in the previous video we saw about Lambda also.

04:15.510 --> 04:21.270
Now I will show you filter operation with uh with multiple condition.

04:21.300 --> 04:21.570
Okay.

04:21.600 --> 04:24.990
So here I'm going to say filter with a lambda function.

04:25.440 --> 04:28.800
Lambda function and multiple conditions.

04:28.800 --> 04:32.520
So just to make you understand that what all things are possible.

04:32.550 --> 04:33.120
Okay.

04:33.120 --> 04:39.360
So multiple conditions now as usual I'll take this numbers okay.

04:39.990 --> 04:40.710
Right.

04:40.710 --> 04:44.730
And let's say I will write a variable even.

04:44.730 --> 04:48.030
And it should be even I'm writing two check okay.

04:48.060 --> 04:50.610
Even and greater than five.

04:50.640 --> 04:50.880
Okay.

04:50.880 --> 04:53.280
So this is my variable that I'm actually going to create.

04:53.310 --> 04:54.900
Two things I'm actually doing over here.

04:54.930 --> 04:57.420
First of all I will check whether the number is even or not.

04:57.420 --> 05:01.130
Will filter out based on the number is even or not, and it should be greater than five.

05:01.160 --> 05:03.320
So in this I will again use list.

05:03.350 --> 05:05.030
I will again use filter.

05:05.060 --> 05:05.600
Okay.

05:05.600 --> 05:11.600
And along with that I will use my lambda x x colon greater than five okay.

05:11.630 --> 05:13.100
So this is my one condition.

05:13.100 --> 05:15.020
And I will go ahead and use one logical.

05:15.020 --> 05:20.420
And so and operation over here I will use and this will be x percentile two or modulus two is double

05:20.420 --> 05:20.990
equal to zero.

05:20.990 --> 05:22.640
So these are my two conditions.

05:22.640 --> 05:23.900
If it is getting satisfied.

05:23.900 --> 05:28.070
And this will get applied to all my numbers list of numbers that is available over here.

05:28.070 --> 05:31.640
So inside this lambda function what we are doing we are applying two expression.

05:31.640 --> 05:38.030
One is x is greater than five and the other one is x modulus two r is double equal to zero.

05:38.060 --> 05:40.100
Right now if I go ahead and print.

05:40.100 --> 05:41.090
So what it is going to do.

05:41.120 --> 05:45.440
First of all it is going to check whether it is even or not and then whether it is greater than five.

05:45.440 --> 05:50.150
So out of this elements you will be able to see that I am able to get only six and eight remaining.

05:50.150 --> 05:52.460
All elements are odd or less than five.

05:52.580 --> 05:53.330
Perfect.

05:53.360 --> 05:57.470
Uh, let's go ahead and see one more way of doing this.

05:57.500 --> 05:57.980
Okay.

05:58.010 --> 06:03.340
Now let's say that I want to apply a filter operation not only on list, let's say on dictionaries.

06:03.370 --> 06:07.060
Okay, so here I will just go ahead and do this.

06:07.090 --> 06:07.510
Okay.

06:07.540 --> 06:19.780
I will say hey apply filter to check if the age is greater than 25 in dictionary.

06:20.260 --> 06:20.920
Okay.

06:21.280 --> 06:25.810
So here you can basically see I will just create some data.

06:25.810 --> 06:27.940
And again I have already created this kind of data.

06:27.940 --> 06:35.410
So let me see from my which functions map function I think I have actually used this kind of data.

06:35.650 --> 06:36.610
Uh yeah.

06:36.610 --> 06:39.070
So this is my people data.

06:39.100 --> 06:39.280
Okay.

06:39.310 --> 06:41.230
So here I have specifically people data.

06:41.260 --> 06:42.820
I will use this same thing.

06:43.990 --> 06:44.560
Okay.

06:45.400 --> 06:47.470
Just trying to save time.

06:47.500 --> 06:48.040
Okay.

06:48.070 --> 06:56.020
Now from this I will create a function where I will say age greater than 25.

06:56.050 --> 06:56.620
Okay.

06:56.620 --> 07:03.920
And, uh, let's say I will just make some more data over here, I will copy, I'll put over here.

07:03.950 --> 07:05.300
I'll say Jack.

07:05.330 --> 07:10.100
Then along with this, I'll say John, age will be 25.

07:10.130 --> 07:10.670
Okay.

07:10.790 --> 07:14.660
And then what we are going to basically do, we are going to create a function here.

07:14.660 --> 07:18.560
We are going to give the person information, one item person information.

07:18.560 --> 07:22.850
And then we are just going to return the person of age.

07:22.850 --> 07:23.720
Right.

07:23.720 --> 07:29.060
And this age should be greater than 25.

07:29.150 --> 07:31.820
And based on this either it will return true or false.

07:31.850 --> 07:35.600
Right now we are just going to apply this with the help of filter.

07:35.600 --> 07:37.970
And we will use this particular function.

07:37.970 --> 07:45.110
So I will say filter and this will be my age greater than 25 along with my people because I need to

07:45.110 --> 07:51.440
apply this on my entire um uh, what to say collection that is iterable.

07:51.440 --> 07:53.510
And then I'll try to convert this into a list.

07:53.510 --> 07:54.830
So if I go ahead and print it.

07:54.830 --> 08:01.060
So here you will be able to see name age age 32 and 33 because this particular condition checks only

08:01.060 --> 08:04.840
and returns only the age based on the age condition greater than 25.

08:04.870 --> 08:06.730
It will return those specific records.

08:06.730 --> 08:10.930
And yes, from this particular filter operation, you will be able to see that I'm able to do this.

08:10.960 --> 08:11.320
Right.

08:11.320 --> 08:14.350
So these are some of the amazing operations that you can do with filter.

08:14.380 --> 08:16.960
So I would definitely try to put some kind of conclusions.

08:16.960 --> 08:21.730
And I'm doing that particular thing with respect to every video that I record in this.

08:21.730 --> 08:24.820
So here these are some of the conclusions.

08:25.300 --> 08:29.740
So here you can see the filter function is a powerful tool for creating iterators that filter items

08:29.740 --> 08:31.960
out of an iterable based on a function.

08:31.960 --> 08:35.860
It is commonly used for data cleaning filter objects and removing unwanted elements.

08:36.100 --> 08:40.000
By mastering filter, you can write more concise and efficient code for processing and manipulating

08:40.000 --> 08:41.140
collections in Python.

08:41.140 --> 08:43.150
So yes, this was about filter.

08:43.180 --> 08:48.100
Now as we go ahead, we will be discussing about and again we will be reusing all these things.

08:48.100 --> 08:51.160
But I think you got a clear idea about functions.

08:51.160 --> 08:52.300
Different types of functions.

08:52.300 --> 08:55.720
Anonymous function Lambda function maps function filter function.

08:56.110 --> 08:57.760
Uh, so yes, this was it from my side.

08:57.760 --> 08:59.560
I will see you all in the next video.

08:59.560 --> 09:00.100
Thank you.

09:00.130 --> 09:00.430
Take care.
