>> A need function but it's sometimes hard to initially
understand in the world of Python is something called Lambdas.
So to understand it,
let's start by looking at the types of
problems that Lambdas can solve.
So let's say I want do something quite simple.
I want to sort some objects
and each object has a couple of different properties to it.
I have Susan, age 50.
Yes, as of this recording,
that is my age, but it's okay.
I'm cool with that. Great thing about being a runner.
When you turn 50,
that means you're at the bottom of your age group.
So instead of being 49 and running against 40-year-olds,
I'm 50, running against 59-year-old.
So we're the only people that I know of.
Runners are great because we
don't care about getting older. It's a good thing.
So I have this list of values here,
Susan, age 50, and Christopher, age 47.
If I just call sort to try and sort that out,
I'm going to get an error back because it doesn't know whether to
sort by the name or to sort by the age.
So you'll actually get an error when you try to run this code.
Now luckily, there's a neat feature with sort.
It can handle the regular types and strings.
But something complicated like this,
there's something we can do called the key perimeter.
When you pass in a key parameter,
you can specify a function that you want to be called
for each element in
the list before it compares the items for sorting.
So basically it's a way for you to say,
"Hey, when you're sorting,
call this function, it'll tell
you what value to look at when you're sorting."
In our case, we just need to specify,
look at the age or look at the name or something like that.
So I could, using the Python skills we learned in the intro
to programming with Python course, define a function.
So in this case, I've got a function called
sorter and you pass into it an item,
and that'll be one of the items in
our list and it will return the item name.
So if I want to sort by name,
I can just specify that this is the function
that tells you what value to look at when sorting.
So we have the same list of values here and when
we call presenters.sort, we say, "Hey,
use the sorter function and for each list item,
it will then return the name."
So now we're effectively saying sort by name.
Now when we run that code wonderfully,
it comes back and returns the list of
values and it shows Christopher first and Susan second,
because Christopher's name comes first in alphabetical order.
So this is pretty neat. You know what?
This works just fine,
but it's a pretty small function.
It's not really doing a whole heck of a lot.
So what Lambdas allow us to do is if you've
got a function like this with just a single line of code,
you don't actually have to declare an explicit function.
You can use something called a Lambda function.
It's done inline in your code.
So instead of saying
key equals sorter and sorter is a function declared up here,
I can just say, "Hey,
let's declare that the key is a function,
and that function is a Lambda function."
When you specify a Lambda function,
you specify a parameter item
and the value to be returned, the item name.
So let me deconstruct that a little bit for you.
So what we do is before we had this function called sorter,
which accepted a parameter item and it
returned the items name property.
When you use a Lambda function,
so your function is, you're just saying declare a Lambda function.
What you'll see is parameter item is the same for both.
So the value you specify here maps to
the parameter that you would have been passing to
the function if you declare to be explicitly.
The value returned is the value you specify after the colon.
So this is basically a function,
Lambda function that accepts
a parameter called item and returns the item name.
Then you can call that,
instead of passing in a function name,
you can just call a Lambda function,
declare the entire function and call it all in one line of code.
You can be more clever than that.
Here I've actually modified the code a little bit,
so it actually returns the length of the name.
So this would actually sort by the length of a name.
So in this case, there will be Susan, then Christopher,
because the name Susan uses
less characters than the name Christopher.
So Lambda function is really clever tool.
Not something you have to use,
but it's something you are going to encounter.
It's a clever trick. It can be
really make for some very clean code.
So let's try that out inside the actual code itself.