Welcome back.
Let's talk about something that I really, really love, and it's called Lambda Expressions.
And Lambda is actually a computer science term that really is compatible with this idea of functional
programming.
You see, lambda expressions in Python are one time anonymous functions that you don't need more than
once.
What does that mean?
Well, lambda expressions are really, really useful when you're using them for functions that a you
only use once.
Remember how we talked about functions are useful because we can define them and then use these functions
over and over and thus save us from copy and pasting code.
Well, lambda functions are for those occasions where we have a function, but we only need to use it
once.
And then the second part is that they're anonymous functions.
That is because we only use them once.
We don't need to have a name for them because I mean, we don't really need to store them anywhere on
our machines.
I just want to use it once.
Just run it and then I'm done with it.
Just throw it away.
So how does a lambda function look like?
A lambda function looks like this.
We say lambda in python.
Let me just add a bit of space here just so we can see.
We say Lambda and you can see that it's highlighted blue.
And then I'm going to say a parameter that this function is going to get, and then a colon and then
some sort of a function here.
And the parameter.
Or instead of function, it'll be some sort of a manipulation that we do.
So let's say action that we want to take.
On a parameter.
All right, this still looks confusing, so let's actually try to implement it in our functions that
we've created.
So let's say multiply by two is one of those things that we're only going to use once.
I only want to multiply by two.
That's my list and then I'm done with it.
I don't need to save it to memory.
It's just extra piece of code.
I don't need this.
How can we do it?
Well, let's say we have the map function back here and let's turn it into a list.
And for now I'm just going to minimize these.
So we only focus on the multiply by two.
And here let's go back to multiply by two.
Remember how we had it before.
My list and we don't need the third parameter just like that.
Now with a lambda function I can say, Hey, I don't need to create a whole new function, I'm only
going to use it once.
Let's just remove this entirely.
And instead of using multiply by two, I'm going to say lambda.
And here I'm going to give it the parameter.
What parameter am I going to receive?
Well, I'm going to receive an item from my list.
And then in here, what do we want to do to my list?
Well, I want to multiply it by two.
All right.
Could this work?
Let's give it a go.
I'm going to run.
Whoa.
Did you see that?
I didn't even use multiply by two.
As a matter of fact, I can just delete it.
And if I click Run, everything still works.
Now.
Why is this?
Remember, Lambda expressions are one time anonymous functions.
There's no name attached to this function.
This is the function right here.
That you don't need to run more than once.
So once the interpreter runs this line of code, it doesn't remember this.
It just forgets about it.
But it performs the action for us.
In our case, we're saying, Hey, I want to create a lambda expression.
I'm going to take an item from my list.
And then I'm going to multiply that item times two and this automatically returns it again.
The order that we do is lambda then.
We give it the pram, Colin, and then the action we want to take.
In our case, it'll be pram.
And whatever action we want to do.
Like.
So in our case, item times two and this automatically returns.
How cool is that?
This is a really nice feature of Python to keep your code really nice and clean and not clutter with
all these functions.
And other languages might not have lambdas, but they are sometimes called anonymous functions.
Again, something that we've discussed why they're anonymous.
They're just like normal functions and even behave like them.
But we only use them once and they don't have a name.
But you can see here that it makes things a little bit cleaner.
What if we try to do filter now with a lambda function?
Well, if you want you can pause the video and try yourself.
But it should be fairly easy, right?
If I do filter.
I can do, Lambda.
Item and then item.
We'll just copy this.
So the action that we want to take on the item is this.
And if I run it.
Again.
It works.
How nice is that?
No longer need this function either.
Very, very nice.
What about reduce?
Can we use reduce with lambda?
Sure.
Why not?
Let's say here we'll say reduce.
We're going to remove the list from here because we're just producing a single value.
I'm going to say Lambda.
Item remember reduce accepts to parameters the accumulator and then the item.
So I can just give it two parameters like this and then say, what do we want to do?
Well, I just want to add accumulator with the item.
So if I do my list here like this and I click Run.
Check that out.
That works as well.
How cool is that?
Again, we no longer need the accumulator and now we just have one line functions that I can just run
and immediately reduce the list.
Very, very cool.
We're going to be exploring lambdas a little bit more, especially in the comprehension part of the
course.
So if you're still not 100% on them, don't worry, we'll have more use cases and why they're useful
later on in the course.
But lambda expressions I really, really like they make your code really, really small.
However, they do make the code a little bit less readable.
So it is a trade off where you can get really clever with lambda expressions but make your code really
confusing to others.
So you do want to be careful.
For now, we're done.
I'll see you in the next video.
Bye bye.