Hello guys.
So we are going to continue our discussion with respect to Python.
And in this video we are going to discuss about the map function.
Okay.
So what exactly is a map function.
It applies a given function to all the items in an input list or any other iterable, and returns a
map object an iterator.
This is particularly useful for transforming data in list comprehensively.
Now, uh, the best example to show you that.
Why do we exactly use map?
Let me just go ahead and write a code for you.
Okay.
So let's say, um, uh, I will just go ahead and define one method which is called as square.
Okay.
So what the square does is that it takes an input and it just multiplies, or it does a square of that
specific input or it multiply it with itself and it give us gives us the result.
Okay.
So if I go ahead and try to put any number over here, let's say square of four is nothing but 16.
If I go ahead and square are square of ten, then I should be getting 100 right now.
This is fine right here.
It's very simple.
I'm just giving an input and I'm doing the square of it.
Okay.
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
can be in the form of tuple, let's say in this particular case I have all these numbers 12345678 okay.
Now I want to apply the same operation for each and every number.
One way is that I may probably give the list directly over here.
Then I may put a for loop, right?
I iterate through each and every element and then probably, you know, do the multiplication and append
in a separate list.
Right.
So for doing this specific operation I need to do so many things right.
I need to create an empty list and again do the append operation inside that iterate all through all
this particular elements.
Now instead of doing all those things you know we can directly use map right now.
Let me do let me show you what does map usually take.
Okay.
Now I want to perform this operations on all these elements.
So map usually takes two input parameters okay.
One is the function name and one is the iterable.
Iterable basically means the collection, right.
So in this particular case if I'm calling map.
So first parameter that I am going to give is square.
So square is my function.
I don't even have to use brackets over here.
And the second thing that I have to give is my iterable.
That is my numbers okay.
So if I go ahead and execute this here, you can see that it has created a map object at this specific
memory location.
Now since I want, let's say all the values in the form of list.
So I will just go ahead and convert this into a list okay.
So if I convert this thing into a list.
So here you'll be able to see the output.
Right now what this is going to do is that it is going to take each and every number.
The map is going to take each and every number, call this particular function.
And uh, basically just for each and every number, it is just going to do the square right now if I
go ahead and see the answer.
So this basically becomes my list over here.
So I have to use this brackets since uh okay.
Now let's go ahead and see this.
So here you can see for every number 149 16, 25, 36, 49, 64.
It has just squared up each and every number.
So this is one basic example of using map.
Right.
So it applies a given function to all the items in an input list.
Uh like in in this particular case it is list or it can be any other iterable.
Okay.
Now this is amazing.
Let me just show you more functionalities.
And this time I will be using Lambda function with map okay.
Lambda function with map and obviously lambda function.
You know that if there is a single expression you can just go ahead and apply lambda.
Okay.
So I will take the same operation.
So let's say this is my numbers okay.
Now how do I apply instead of this particular function I'll apply lambda function and I'll give the
same name number.
So again I will go ahead and write list.
And here I'm going to use the map function.
The first parameter will be my lambda function.
Lambda function is nothing but it is an anonymous function.
So I will just go ahead and write lambda.
Now what is the kind of operation that I really want to do?
So input argument will be one x parameter let's say any experiment.
And here I'm just going to multiply x into x.
Right.
And this is my entire lambda function itself right.
Now the second parameter is nothing but my numbers which is my list.
Now if I go ahead and execute it here, you'll be able to see with the help of lambda function.
Also we are able to execute this okay.
Now this is also very much simple very much easy.
You can here you can write any kind of operation.
Let's say I want to just add ten I want to add 100.
I want to do the multiplication division anything that I can I really want to do I'll be able to do
it okay.
Now let's say that okay fine.
This is fine okay.
Can we map multiple iterables.
Now the question is can we map multiple iterables.
Okay.
And that is what we are going to discuss over here.
So let me go ahead and write numbers one okay.
Numbers one.
And here I'm just going to create 123.
And then I'm just going to write numbers.
Numbers to which is equal to four comma five comma six okay.
Now since I have two iterables over here.
And what I really want to do is that take each and every element from this iterable and keep on adding
both of them.
Let's say my final output should be a list of elements where I'll be adding the first element, second
element, and third element.
So my final answer should be five, seven, nine.
Okay, so can I do that with the help of map and lambda function.
So here I'm just going to go ahead and create added underscore numbers okay.
And here I'm going to just go ahead and use map.
Along with that I'm going to use my Lambda function.
Let's say I'm going to take two parameters right.
This will be one parameter.
This will be the other parameter.
So I will just go ahead and use x comma y.
And now my expression will be very simple right.
I'm just going to add both this number.
So it should be x plus y right now the second parameter that I am going to give is my numbers one right.
So it will be my numbers one.
Now second parameter.
Also I can actually give right.
So second parameter will be my next list right.
So here I'm just going to go ahead and paste number two.
And this all needs to be converted in list Okay.
And now if I go ahead and print my added numbers.
So here you can see that I'm able to get five comma seven comma nine.
Right.
So this is one amazing example.
Uh directly uh what I'm actually doing now there are multiple things that we can do with map.
Let's show you one more operation.
So here now I try to use map to convert a list of string to integers.
Okay I'm going to do this okay.
Now let's say that I have this entire code okay I'll just show you this code.
Very simple.
So here is my string numbers.
What I'm actually doing I am just applying map.
The function will be int.
So typecasting is basically doing done on all the string underscore numbers which is in the form of
list.
And here you can actually see I'll be able to get the output.
So there are a lot of options that you can specifically do with the help of uh map.
Again you can use inbuilt function like int like uh string dot upper, anything that you really want
to do.
And one thing you have to make sure whenever you are using function I don't have to use any brackets.
Right.
So lambda function I've called square I have called square is already defined over here.
I don't have to use any brackets.
Okay.
Now let me just show you one more, uh, good example that how I can apply inbuilt function with the
help of map.
So let's say these are my words okay.
The first word is apple.
And let's say that these all are in smaller character.
Then I have banana.
Then I have cherry okay.
Now what I'm actually going to do I'm just going to create my upper words.
And here I'm going to use list of map.
Okay.
And now I know that in string there is a method is called as dot upper.
And I need to apply this str dot upper on each and every words.
Right.
So I'll just go ahead and write words.
So once I do this and once I print my upper underscore words or word, I'll be able to see this.
See apple, banana and cherry is there.
Okay.
Uh, let me just do one more thing.
One more, uh, best thing, uh, over here, let's apply to a list of dictionary.
Let's apply a map function to a list of dictionary.
So I will go ahead and create a function which will be saying get underscore name.
Okay.
So here I will be giving my dictionary okay.
This person will be a dictionary and I will return person of name.
Okay, now this is fine for one dictionary item, right?
But what if I have a list of dictionary item?
Let's say that I have a list of dictionary item which I will define over here.
So let's say I will go ahead and write people.
And uh, let me quickly go ahead and define over here and let me go and write name colon.
Okay.
And uh, name will basically be crush comma age.
Age will be nothing but uh, colon 32.
Okay.
So this will be my first, uh, store name item or people item.
And then let me just go ahead and say name.
Colon is equal to Jack.
Okay.
And then I have the age as 33.
Okay.
So this will be my second item okay.
Now I want to perform this only not only to one item, instead all the items.
Right.
So let's say I have just taken two over here.
Now how do I do it?
First of all, I will use list.
Then I will use map.
The map first function will be get underscore name.
Okay then I'll be applying this to my entire iterable.
And here I have list of dictionary items right.
So if I go ahead and see the output here, you can see from this two records I have explored Christian
Jack.
And it is being given me in the form of list.
Right.
Uh, this is one amazing things that you'll be able to see over here.
Um, and uh, at the end of the day, uh, you can apply even multiple functions if you want.
You can create multiple function apply to the same list of elements.
So in conclusion, uh, if I really want to talk about map, uh, so here, uh, one thing is that a
map is used, uh, it is used in various places.
So I'll just give some kind of conclusion over here.
So map function is a powerful tool for applying transformation to iterable data structures.
It can be used with regular functions Lambda function, even multiple iterables providing a versatile
approach to data processing in Python.
So by understanding utilizing map, you can write more efficient and readable code.
It is quite efficient, you know, in just a single line things without iterating.
You're using this particular efficient data structures.
And at the end of the day, you just need to make sure your code is much more readable.
The efficiency is there, performance is there with respect to this.
So I hope you like this particular video.
This was about maps function.
I'll see you in the next video.
Thank you.