Welcome back.
I want to talk to you about some useful functions that we get that actually allow us to think in a functional
programming paradigm.
The name of these functions are map filter.
Zip.
And reduce.
And these are very, very useful, very common functions that you'll write a lot when you're a Python
programmer.
So let's explore them.
First one, we're going to start with Map.
And map actually allows us to simplify the code that we have here of multiply by two.
You see with map, I have it available as a function, as a built in function.
And we see here that we give the first parameter a function, and then the second parameter is an iterable.
A good way to think about it is we want to take some sort of action here.
So a function and then the data that we want to take action upon.
So let's say in our case, one, two, three.
So what action do we want to take on?
One, two, three.
Well, we want to multiply by two, so let's say multiply.
By two.
Now I have to move this down because we have to define the function first.
And run it like this.
So instead of printing this, let's do.
Print.
Like this if I run it.
I get a map object at this memory location.
And MAP automatically gives us this object that it has created in this memory.
So in order for us to actually view it, we have to turn it into a list like this.
So if I run this.
It says on line for int'l object is not iterable.
Well, it's because with map we no longer need to do this creation of a list than appending to a list.
You see, the nice thing about map is that I give it some data and then this data gets acted upon by
this.
And all we need to do with the map function is to have a function that returns.
This part item times two.
So instead of having all of this code, all we need to do is say return.
Item.
Times two.
So let's change this parameter to item.
And look at the code a little bit more in detail.
So with the map I say, Hey, I want you to run the function.
Multiply by two.
Notice here that I don't even have the brackets because I don't need to call it map does it for me.
Map just says, Hey, just give me the function, I'll take care of the rest.
So I'm not calling the function here.
The map is going to call the function for me.
I'm just saying, hey, when I call map like this.
I want you to use this function at this memory address and use this data.
Remember our functional programming paradigm.
We have data that gets acted upon, so we separate those two out.
Now this function is going to take each one of the items in our intervals.
In our case, first time around item will be one, then two, then three.
And then all we need to do is to return what effect we want to have on that item.
In our case, it's two.
So by doing this map automatically runs this function for us and loops through all the items in the
iterable and returns for us a new map object that we're going to convert into a list if I run this.
I get two for six?
Now, here's the neat part.
If I do, let's say my list and I say, one, two, three.
And I changed my list over here.
Like this.
And at the end, also print my list.
If I click Run.
You'll see that my list hasn't changed.
Remember?
A pure function is a function that doesn't affect the outside world.
And this way map allows us to create a whole new list.
That doesn't modify my list.
So my list is immutable, right?
We don't change it.
And all it does is map function, takes care of it and returns a new list for us.
But we don't affect the outside world.
There's no side effects in this function.
We're simply just multiplying item to item by two, but creating a new list out of this.
And every time we give it the same input, it's going to result in the same output.
Very, very cool.
And map is extremely useful because any time we have something that we can iterate over and we want
to change.
Let's say we have emails or usernames and we need to update the usernames to, let's say, be all capital
letters.
In that case, we can iterate and using the map function, change all the usernames in our list to let's
say, capital letters.
MAP is going to be one of those functions that you use a lot in your programming career.
And I hope you can see here how clean and nice it is, again, using functional programming paradigms.
Let's move on to the next one.
Let's talk about filter.
Next.