WEBVTT

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

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

00:03.650 --> 00:06.890
And in this video we are going to discuss about the map function.

00:06.920 --> 00:07.490
Okay.

00:07.490 --> 00:09.380
So what exactly is a map function.

00:09.380 --> 00:16.730
It applies a given function to all the items in an input list or any other iterable, and returns a

00:16.730 --> 00:18.470
map object an iterator.

00:18.500 --> 00:22.610
This is particularly useful for transforming data in list comprehensively.

00:22.670 --> 00:25.580
Now the best example to show you that.

00:25.730 --> 00:27.800
Why do we exactly use map?

00:27.800 --> 00:29.720
Let me just go ahead and write a code for you.

00:29.750 --> 00:30.260
Okay.

00:30.260 --> 00:38.090
So let's say uh, uh, I will just go ahead and define one method which is called as square.

00:38.120 --> 00:38.600
Okay.

00:38.600 --> 00:46.910
So what the square does is that it takes an input and it just multiplies, or it does a square of that

00:46.910 --> 00:50.240
specific input or it multiply it with itself.

00:50.240 --> 00:52.040
And it gives us gives us the result.

00:52.070 --> 00:52.490
Okay.

00:52.490 --> 00:58.040
So if I go ahead and try to put any number over here, let's say square of four is nothing but 16.

00:58.070 --> 01:03.080
If I go ahead and square R square of ten, then I should be getting 100 right now.

01:03.080 --> 01:04.730
This is fine right here.

01:04.730 --> 01:05.480
It's very simple.

01:05.480 --> 01:07.820
I'm just giving an input and I'm doing the square of it.

01:07.850 --> 01:08.420
Okay.

01:08.480 --> 01:16.730
Now let's say if I have a list of numbers or let's say I have a numbers in the form of list, or it

01:16.730 --> 01:18.110
can be in the form of tuple.

01:18.110 --> 01:22.340
Let's say in this particular case I have all these numbers 12345678.

01:22.370 --> 01:23.000
Okay.

01:23.570 --> 01:29.210
Now I want to apply the same operation for each and every number.

01:29.390 --> 01:33.080
One way is that I may probably give the list directly over here.

01:33.080 --> 01:35.450
Then I may put a for loop, right?

01:35.480 --> 01:40.880
I iterate through each and every element and then probably, you know, do the multiplication and append

01:40.880 --> 01:41.900
in a separate list.

01:41.930 --> 01:42.440
Right?

01:42.440 --> 01:45.560
So for doing this specific operation I need to do so many things right.

01:45.560 --> 01:50.060
I need to create an empty list and again do the append operation inside that iterate all through all

01:50.060 --> 01:51.320
this particular elements.

01:51.410 --> 01:57.650
Now instead of doing all those things, you know we can directly use map right now let me do let me

01:57.650 --> 01:59.450
show you what does map usually take.

01:59.480 --> 02:00.050
Okay.

02:00.050 --> 02:03.310
Now I want to perform this operations on all these elements.

02:03.310 --> 02:06.970
So map usually takes two input parameters.

02:07.000 --> 02:07.450
Okay.

02:07.480 --> 02:11.440
One is the function name and one is the iterable.

02:11.470 --> 02:13.120
Iterable basically means the collection.

02:13.150 --> 02:13.630
Right.

02:13.630 --> 02:16.120
So in this particular case if I'm calling map.

02:16.120 --> 02:18.730
So first parameter that I am going to give is square.

02:18.730 --> 02:20.080
So square is my function.

02:20.080 --> 02:22.210
I don't even have to use brackets over here.

02:22.330 --> 02:25.360
And the second thing that I have to give is my iterable.

02:25.360 --> 02:26.800
That is my numbers okay.

02:26.830 --> 02:31.240
So if I go ahead and execute this here, you can see that it has created a map object at this specific

02:31.240 --> 02:31.930
memory location.

02:31.960 --> 02:35.620
Now since I want let's say all the values in the form of list.

02:35.620 --> 02:38.920
So I will just go ahead and convert this into a list.

02:38.950 --> 02:39.520
Okay.

02:39.520 --> 02:41.800
So if I convert this into a list.

02:41.800 --> 02:44.320
So here you'll be able to see the output right.

02:44.350 --> 02:47.650
Now what this is going to do is that it is going to take each and every number.

02:47.680 --> 02:51.370
The map is going to take each and every number, call this particular function.

02:51.370 --> 02:55.540
And basically just for each and every number it is just going to do the square right.

02:55.570 --> 02:57.430
Now if I go ahead and see the answer.

02:57.850 --> 03:00.700
So this basically becomes my list over here.

03:00.700 --> 03:04.090
So I have to use this brackets since, uh.

03:04.120 --> 03:04.570
Okay.

03:04.570 --> 03:06.250
Now let's go ahead and see this.

03:06.250 --> 03:10.840
So here you can see for every number one, four, nine, 16, 25, 36, 49, 64.

03:10.840 --> 03:12.880
It has just squared up each and every number.

03:12.880 --> 03:16.510
So this is one basic example of using map.

03:16.510 --> 03:16.870
Right.

03:16.870 --> 03:20.950
So it applies a given function to all the items in an input list.

03:20.950 --> 03:25.060
Uh like in in this particular case it is list or it can be any other iterable.

03:25.090 --> 03:26.920
Okay now this is amazing.

03:26.920 --> 03:28.720
Let me just show you more functionalities.

03:28.720 --> 03:32.230
And this time I will be using Lambda function with map okay.

03:32.260 --> 03:35.680
Lambda function with map and obviously lambda function.

03:35.680 --> 03:39.910
You know that if there is a single expression you can just go ahead and apply lambda.

03:39.940 --> 03:40.300
Okay.

03:40.330 --> 03:41.980
So I will take the same operation.

03:41.980 --> 03:44.140
So let's say this is my numbers okay.

03:44.230 --> 03:46.060
Now how do I apply.

03:46.120 --> 03:50.530
Instead of this particular function I'll apply lambda function and I'll give the same number.

03:50.530 --> 03:52.510
So again I will go ahead and write list.

03:52.510 --> 03:54.760
And here I'm going to use the map function.

03:54.760 --> 03:57.730
The first parameter will be my lambda function.

03:57.730 --> 04:00.310
Lambda function is nothing but it is an anonymous function.

04:00.310 --> 04:02.440
So I will just go ahead and write lambda.

04:02.470 --> 04:05.320
Now what is the kind of operation that I really want to do?

04:05.320 --> 04:10.000
So input argument will be one x parameter, let's say any x parameter.

04:10.000 --> 04:13.000
And here I'm just going to multiply x into x right.

04:13.000 --> 04:18.160
And this is my entire lambda function itself right now the second parameter is nothing but my numbers

04:18.160 --> 04:18.940
which is my list.

04:18.970 --> 04:22.510
Now if I go ahead and execute it here, you'll be able to see with the help of lambda function.

04:22.510 --> 04:24.700
Also we are able to execute this okay.

04:24.730 --> 04:27.370
Now this is also very much simple very much easy.

04:27.400 --> 04:29.440
You can here you can write any kind of operation.

04:29.440 --> 04:31.180
Let's say I want to just add ten.

04:31.210 --> 04:32.140
I want to add 100.

04:32.170 --> 04:36.700
I want to do the multiplication division anything that I can I really want to do I'll be able to do

04:36.700 --> 04:36.940
it.

04:36.970 --> 04:37.510
Okay.

04:37.570 --> 04:39.580
Now let's say that okay fine.

04:39.580 --> 04:40.600
This is fine.

04:40.630 --> 04:41.020
Okay.

04:41.050 --> 04:43.810
Can we map multiple iterables.

04:43.840 --> 04:48.910
Now the question is can we map multiple iterables?

04:48.940 --> 04:49.420
Okay.

04:49.420 --> 04:51.490
And that is what we are going to discuss over here.

04:51.490 --> 04:53.890
So let me go ahead and write numbers one okay.

04:53.920 --> 04:55.180
Numbers one.

04:55.180 --> 04:57.880
And here I'm just going to create 123.

04:57.910 --> 05:01.060
And then I'm just going to write numbers.

05:01.090 --> 05:06.040
Numbers two which is equal to four five comma six.

05:06.070 --> 05:06.610
Okay.

05:07.060 --> 05:13.600
Now, since I have two iterables over here, and what I really want to do is that take each and every

05:13.600 --> 05:15.970
element from this iterable and keep on adding both of them.

05:15.970 --> 05:20.770
Let's say my final output should be a list of elements where I'll be adding the first element, second

05:20.770 --> 05:21.610
element, and third element.

05:21.610 --> 05:24.370
So my final answer should be 579.

05:24.400 --> 05:24.790
Okay.

05:24.820 --> 05:28.150
So can I do that with the help of map and lambda function.

05:28.180 --> 05:32.620
So here I'm just going to go ahead and create added underscore numbers.

05:33.190 --> 05:33.700
Okay.

05:33.700 --> 05:38.020
And here I'm going to just go ahead and use map.

05:38.170 --> 05:40.990
Along with that I'm going to use my lambda function.

05:40.990 --> 05:43.450
Let's say I'm going to take two parameters right.

05:43.480 --> 05:44.620
This will be one parameter.

05:44.650 --> 05:45.760
This will be the other parameter.

05:45.760 --> 05:47.860
So I will just go ahead and use x comma y.

05:47.860 --> 05:50.230
And now my expression will be very simple right.

05:50.230 --> 05:52.390
I'm just going to add both these numbers.

05:52.390 --> 05:54.340
So it should be x plus y right.

05:54.370 --> 05:59.530
Now the second parameter that I'm going to give is my numbers one right.

05:59.530 --> 06:01.450
So it will be my numbers one.

06:01.480 --> 06:02.770
Now second parameter.

06:02.770 --> 06:04.090
Also I can actually give right.

06:04.090 --> 06:05.940
So second parameter will be my next list.

06:05.970 --> 06:06.450
Right.

06:06.450 --> 06:09.570
So here I'm just going to go ahead and paste number two.

06:09.600 --> 06:13.200
And this all needs to be converted in list.

06:13.500 --> 06:14.130
Okay.

06:14.130 --> 06:17.850
And now if I go ahead and print my added numbers.

06:18.330 --> 06:22.290
So here you can see that I'm able to get five comma seven comma nine.

06:22.320 --> 06:22.830
Right.

06:22.830 --> 06:24.990
So this is one amazing example.

06:25.020 --> 06:30.750
Uh directly uh what I'm actually doing now there are multiple things that we can do with map.

06:30.750 --> 06:32.760
Let's show you one more operation.

06:32.760 --> 06:44.250
So here now I try to use map to convert a list of string to integers okay I'm going to do this okay.

06:44.280 --> 06:48.510
Now let's say that I have this entire code okay I'll just show you this code.

06:48.510 --> 06:49.440
Very simple.

06:49.440 --> 06:51.420
So here is my string numbers.

06:51.420 --> 06:53.880
What I'm actually doing I am just applying map.

06:53.880 --> 06:54.990
The function will be int.

06:55.020 --> 07:00.750
So typecasting is basically done on all the string underscore numbers which is in the form of list.

07:01.200 --> 07:04.410
And here you can actually see I'll be able to get the output.

07:04.410 --> 07:09.720
So there are a lot of options that you can specifically do with the help of a map again.

07:09.750 --> 07:15.390
You can use inbuilt function like int, like string, dot up or anything that you really want to do.

07:15.390 --> 07:19.830
And one thing you have to make sure whenever you are using function I don't have to use any brackets.

07:19.860 --> 07:20.070
Right.

07:20.100 --> 07:24.210
So lambda function I've called square I have called square is already defined over here.

07:24.210 --> 07:25.650
I don't have to use any brackets.

07:25.680 --> 07:26.220
Okay.

07:26.250 --> 07:32.160
Now let me just show you one more good example that how I can apply inbuilt function with the help of

07:32.160 --> 07:32.640
map.

07:32.640 --> 07:35.370
So let's say these are my words okay.

07:35.400 --> 07:37.410
The first word is apple.

07:37.410 --> 07:40.230
And let's say that this is all are in smaller character.

07:40.230 --> 07:41.670
Then I have banana.

07:41.700 --> 07:44.370
Then I have cherry.

07:44.400 --> 07:49.110
Okay, now what I'm actually going to do I'm just going to create my upper words.

07:49.110 --> 07:54.360
And here I'm going to use list of map okay.

07:54.360 --> 07:59.040
And now I know that in string there is a method is called as dot upper.

07:59.040 --> 08:05.850
And I need to apply this str dot upper on each and every words.

08:05.850 --> 08:06.330
right?

08:06.360 --> 08:07.920
So I'll just go ahead and write words.

08:07.920 --> 08:17.550
So once I do this and once I print my upper underscore words uh or word, I'll be able to see this.

08:17.580 --> 08:20.190
See apple, banana and cherry is there.

08:20.220 --> 08:20.790
Okay.

08:21.150 --> 08:24.180
Uh, let me just do one more thing.

08:24.180 --> 08:28.350
One more, uh, best thing, uh, over here, let's apply to a list of dictionary.

08:28.350 --> 08:30.540
Let's apply a map function to a list of dictionary.

08:30.540 --> 08:34.530
So I will go ahead and create a function which will be saying get underscore name.

08:34.560 --> 08:34.770
Okay.

08:34.800 --> 08:37.020
So here I will be giving my dictionary okay.

08:37.050 --> 08:38.640
This person will be a dictionary.

08:38.670 --> 08:45.300
And I will return person of name okay.

08:45.330 --> 08:48.780
Now this is fine for one dictionary item right.

08:48.780 --> 08:51.600
But what if I have a list of dictionary item.

08:51.600 --> 08:55.800
Let's say that I have a list of dictionary item which I will define over here.

08:55.800 --> 08:57.840
So let's say I will go ahead and write people.

08:57.930 --> 09:07.230
And uh, let me quickly go ahead and define over here and let me go and write name colon Okay.

09:07.260 --> 09:13.830
And name will basically be Krish comma age.

09:14.850 --> 09:24.390
Age will be nothing but a colon 32 okay, so this will be my first, uh, store name item or people

09:24.390 --> 09:24.870
item.

09:24.870 --> 09:30.690
And then let me just go ahead and say name colon is equal to Jack.

09:31.200 --> 09:31.800
Okay.

09:31.830 --> 09:36.390
And then I have the age as 33 okay.

09:36.420 --> 09:38.490
So this will be my second item okay.

09:38.670 --> 09:43.860
Now I want to perform this only not only to one item, instead all the items.

09:43.860 --> 09:44.130
Right.

09:44.130 --> 09:46.200
So let's say I have just taken two over here.

09:46.230 --> 09:47.400
Now how do I do it?

09:47.400 --> 09:48.870
First of all I will use list.

09:48.870 --> 09:50.760
Then I will use map.

09:50.760 --> 09:53.490
The map first function will be get underscore name.

09:53.520 --> 09:54.090
Okay.

09:54.180 --> 09:57.180
Then I'll be applying this to my entire iterable.

09:57.180 --> 09:59.550
And here I have list of dictionary items right.

09:59.550 --> 10:04.170
So if I go ahead and see the output here, you can see from this two records I have explored Christian

10:04.170 --> 10:04.470
Jack.

10:04.470 --> 10:07.080
And it is being given me in the form of list.

10:07.220 --> 10:07.910
right?

10:07.940 --> 10:12.050
Uh, this is one amazing things that you'll be able to see over here.

10:12.320 --> 10:17.660
Um, and, uh, at the end of the day, you can apply even multiple functions if you want.

10:17.660 --> 10:21.080
You can create multiple function, apply to the same list of elements.

10:21.080 --> 10:27.860
So in conclusion, uh, if I really want to talk about map, uh, so here, uh, one thing is that a

10:27.860 --> 10:28.970
map is used.

10:29.000 --> 10:30.620
It is used in various places.

10:30.620 --> 10:33.320
So I'll just give some kind of conclusion over here.

10:34.130 --> 10:38.450
So map function is a powerful tool for applying transformation to iterable data structures.

10:38.450 --> 10:43.250
It can be used with regular functions Lambda function, even multiple iterables providing a versatile

10:43.250 --> 10:45.050
approach to data processing in Python.

10:45.050 --> 10:48.800
So by understanding utilizing map you can write more efficient and readable code.

10:48.830 --> 10:53.990
So it is quite efficient, you know, in just single line things without iterating.

10:54.020 --> 10:56.420
You're using this particular efficient data structures.

10:56.420 --> 11:01.130
And at the end of the day, you just need to make sure your code is much more readable.

11:01.130 --> 11:03.980
The efficiency is there, performance is there with respect to this.

11:03.980 --> 11:05.630
So I hope you like this particular video.

11:05.660 --> 11:07.160
This was about maps function.

11:07.160 --> 11:08.480
I'll see you all in the next video.

11:08.480 --> 11:08.780
Thank you.
