okay let's take a look at lambda
functions inside Python code but I said
to understand lambda functions it
sometimes helps to look at the types of
problems that they can help solve so I
have here I've declared this presenters
list that has post Susan and Christopher
has our name and her age on it but if I
try to sort this list I run into a
problem because the sort function
doesn't know whether to sort by the name
or by the age so if I actually go and
run this code and I know it's gonna fail
hits my file name here then you'll
actually see it comes back and says oh
no you're getting a tight there here I
don't know how to compare the less then
it doesn't know how to compare the two
objects because it doesn't know whether
to look at the age the the combination
of name and age it's just messed up so
it doesn't know how to do the sorting
now sort by itself actually has a
parameter you can use to simplify life
so one of the things you can do when you
call sort is you can specify the key
parameter and you pass it a function
name and not whatever function you pass
to it it will basically take the item in
the list pass that to the function and
the function then can return the value
you want to use for sorting so by
specifying key equals sorter here it now
calls Miss sorter function passes in the
first item in the list
Susan age 50 and says oh well for Susan
age 50 the name is Susan so sort by name
Susan and then it does the same thing
for Christopher and it returns
Christopher so now it knows to sort by
the names Susan and Christopher so now
if I present that sorted list let's run
this code just to make sure I'm telling
the truth here and if we call that you
will see it will successfully sort the
records by name so Christopher shows
first and Susan so second so it
successfully done that sort this works
great and you can write your code this
way but now let's take a look at how
lambdas can make this a little more
elegant a little more compact so I'm
gonna do exactly the same thing this
time I'm going to use a lambda so I
remember we had this presenters dot sort
which was calling with a key of sorter
which was an explicitly declared
function
but now I'm replacing that with one line
of code I'm no longer declaring a
function that's explicitly named instead
my function is something called a lambda
function and a lambda function is
defined in a single line the first value
specify is the name of the parameter so
this is going to accept a parameter
called item and it will return the item
name so the value C here is the value
returned by the function the value here
is the name of the parameter if it's
passed into the function so this is
actually doing exactly the same thing
that our sorter function was doing over
here accepting a parameter called item
and returning item name but instead of
declaring that function explicitly I'm
saying make me a lambda function with
this parameter and this return value and
when you run it it does exactly the same
thing and just to show but you can do
more that than just return a property
there's actually a second one down here
where I've declared a second lab2
function but this time I'm sorting based
on the length of the name so based on
the number of characters but the name
has so if we run this code what we'll
see just make her a nice clean screen to
start with here you'll see the first
thing one is when we have it sorted
alphabetically where our lambda function
was just returning the item name it
sorts by chrystopher first then Susan
because in alphabetical order
Christopher comes before Susan when I
sort it by length it sorts it by Susan
first because the name Susan has less
characters in it than Christopher so
lambda functions you can go for years
with Python and never use them but
they're a very elegant solution for
different types of code and something
you might want to explore and something
you're probably going to encounter the
more you play with Python