Let's do a fun exercise to test our understanding and we're going to use lambda expressions.
Now I want you to create a lambda expression that is going to square our list.
So let's say we have my list and this is my list.
We'll have five, four, three.
Now I want you to create a one line lambda expression that's going to print for us.
A squared list.
So five to the par of two will be 25.
Four to the power of two will be 16 and three to the power of two will be nine.
So a list that contains those values using lambda expressions.
So that's your first challenge is to square.
The next challenge.
And this one's a little tough.
We're going to do some list sorting.
Let's do square here just so you can see.
And in this list, sorting.
I have a list.
And this list.
Well, it's a tough one.
We have a list that is a tuple of zero to.
Then a tuple, let's say, for three.
And a tuple, let's say nine, nine, and then finally a tuple that is ten negative one.
Now, what I want you to do is to sort.
This based on the second.
Value.
Make sure this is a comma.
So that is I want this to be sorted so that this tuple is first because minus one is the lowest, then
this tuple because two is the next highest.
Then this tuple.
So how can you do that using lambda expressions?
Now, this is a tough one.
So if you don't get it, don't worry.
Pause the video here.
Take your time.
See if you can solve this.
Otherwise, I'm going to get going with the answers.
This one should be fairly easy to you, right?
We can just simply do a map function or you know what?
Let's just say new list is going to equal a list that we're going to map over.
And in here, nothing we haven't seen before.
I'm going to say Lambda.
Number is going to be number to the power of two.
And the list that I'm going to give it.
Will be the my list.
If I run this.
There you go.
Nice and easy.
Nice.
Easy.
One liner.
What about this one, though?
This one's a little tough and you really have to think about it.
We've learned that we have the dot sort method, right?
And the sort, actually.
Well, let's see what happens if I just sort like this.
If I do print a dot sort and I click run.
I get none because it modifies the list in place.
So let's just print a after we have sorted it.
If I click Run.
All right.
It.
I mean, it looks like it's sorted with the first guy.
What if we move this?
And here in the middle.
And Click Run.
All right.
Looks like it's sorting it for me, but it's sorting it based on this first item.
But I want this second item or what we call the second key to be the sorting factor.
And if you see here, if we do sort, I have to give it a key.
And we want the key to be.
To.
So how can we do that?
Well, we can do something like this.
We can say key is going to equal.
Each one of the second keys and we can use a lambda expression here.
We can say lambda.
So the default parameter.
Would be lambda of x and then simply say x one.
Hmm.
Let's go over this.
What I'm saying is, hey, I want you to run the sort function.
And the key for the sword function when I'm sorting through everything is I want it to iterate over
each item.
That I'm going to get that is X is going to be this tuple.
And I want you to use the value which we're going to return, which is the second one.
So the key is always going to be by the second item.
Now if we run this.
Look at that.
We're able to sort.
By the second item.
Now, if he didn't get this, don't worry.
This is quite tough and it's hard for you to get used to the syntax or know that sort worked in this
way.
But the goal is to show you that this is a common thing that you can do with lambda functions.
And a way to make sure that you can sort with different keys.
For example, this would work with a dictionary or another list inside of a list.
This is a common way to adjust your sorting, and it's a pattern that you just have to remember.
But it shows you the power of lambda, where we don't really have to write these complicated functions
if we really just needed a one time function.
And just a function that returns something simple.
Lambda is a great tool to use, but if you didn't get this, like I said, don't worry.
This is one of those things that you'll just have to remember.
Let's take a break and I'll see you in the next one.
Bye bye.