Instructor: Hi guys, and welcome back.
In this video, we're going to learn about lambda functions.
A lambda function is a different type of function
which doesn't have a name,
and is only used to return values.
Remember that we mentioned that functions can be used
to perform actions such as printing something out,
or operating with values and returning them,
such as the sum function, or they can do both.
But lambda functions are exclusively used
to operate on inputs and return outputs.
They are almost never used to perform actions.
Let's start with a simple function that gets some input
and returns some output.
Let's go to our trusty add function
that returns x + y.
If you print add of five and seven,
you've got that add five and seven
is going to run this line here,
add x and y together, which gives you 12,
and then it's gonna return it,
replacing essentially this entire function call by
the value 12, and then we're going to print that out.
So, if we press play on this, you get 12.
You can rewrite this function as a lambda function,
which would be like this.
Lambda x, y: x + y.
So what we've got here,
and ignore this underline for a second,
what we've got here is the lambda keyword,
then we've got a space, and then we've got
the list of parameters, which in this case are x and y,
then we've got a colon, and then we've got
the return value of the function,
but we don't specify the return keyword.
So lambda functions are used to return output,
so you don't need to specify return because
that's what they're for, so Python will infer that.
So, this does exactly the same thing as the add function
earlier, but it doesn't have a name.
Notice that the add variable is never created.
So to solve that problem, if you want your lambda functions
to have a name, which sometimes you won't
and I'll show you why not in a moment,
you can just assign that to a variable.
So here we've got the add variable,
make it equal to our lambda function,
and then you can call it in your programme using add.
So again, there are four parts to a lambda function.
You've got the lambda keyword,
then you've got your parameter list,
then you've got your colon,
and finally, your return value without the return keyword.
Lambdas are meant to be short functions,
often used without giving them a name,
for example in conjunction with the
built in function called map.
So we're going to learn about map in a moment.
However, you can give them a name if you want.
If you didn't want to give it a name, then you would have
to use it in the same line where you define it.
Otherwise as soon as you define this function,
you have no way of actually using it,
because it doesn't have a name, so it's
kind of pointless to just define
the lambda function without using it.
If you wanted to use this lambda function,
you need to surround it in brackets
so that Python will treat this as single unit,
and then call the function here, say with
the values five and seven,
and this here means that you are using
the five and seven call on this thing,
which is a lambda function, which essentially
calls the function with the arguments five and seven.
You can see why this is also not very often done,
because it's extremely confusing, and this will still
not do anything, 'cause you still need
your print function around it as well.
So lambda functions used in this way,
not very common at all.
Usually you will define a proper function
and use it like we did earlier on instead.
But let's get into some uses for lambda functions
that are actually useful.
Let's say you've got a function called double
that doubles a number passed into it,
and then you've got a sequence of numbers,
such as one, three, five, and nine.
And you want to double this sequence, or you want
to double each number in this sequence.
Something that you may see is something like doubled equal,
and then use a list comprehension, x * 2 for x in sequence.
So x * 2 is the same as this function returns
for its given input, so instead we can do
double x for x in sequence.
The benefit of this is that using a function call
allows you to potentially perform larger
or more complicated mathematical expressions
without making your list comprehension more complicated.
This specific point in time it's not a big difference
but it can be in other scenarios.
So instead of doing this list comprehension,
you can use the map function.
And that is where lambda functions become extremely useful.
So instead of using that, you can do
doubled = map(double, sequence).
And that does exactly the same as this list comprehension.
What map does is it goes over each value in the sequence
such as a list, a double, a set, and so on,
and it applies the double function on each element,
and then returns the final sequence out to you.
So that's what map is for,
it's very useful, so it is the counterpart
for this list comprehension here.
In Python, you would usually use list comprehensions,
but many other programming languages don't have
list comprehensions, so they have to use things like map.
So, if you are working with developers familiar
with other programming languages, and you want
to use a programming style that they will be familiar with,
you can use something like map.
In addition, if you're working in a large project
and you are using multiple different programming languages,
such as JavaScript as well as Python,
you may want to use map so that your Python code
and your JavaScript code both have similar style.
With that said though, the list comprehension
is usually slightly faster, so there is
no reason to use things like map
over list comprehensions most of the time in Python.
Let's re-write this example using a lambda function.
Your doubled list comprehension here could define
a lambda function here, lambda x: x * 2,
and then use x as the argument to that function,
and then you no longer need this doubled there,
but this is getting quite confusing,
because as I mentioned earlier on, you have your
lambda function, which must be surrounded by brackets,
and then you have another pair of brackets
to pass in the argument, so this is a little bit unreadable.
So if you want to use lambda functions
you should use map instead, just because
it makes it a little bit clearer without so many brackets
what's going on.
By the way, if you want to run this code
and check the value returned by map,
you need to turn it into a list in order to be able
to print it out, just because the map function
doesn't return a list of numbers,
it will return something called a map object.
We're going to learn more about what those things are
as we move through these videos,
but for now just turn it into a list
if you want to see the values in your console.
So just a recap, lambdas are functions without a name.
You can give them a name by assigning them to a variable,
but they don't have a name in the function definition.
They are used to return a value calculated
from its parameters, and they're almost
never used to perform actions.
They're almost always single line, so you don't want
to be doing anything really complicated in them,
and one of the big reasons why they're single line
is because their syntax gets really complicated
to read as they get longer.
Very often it's better to define a normal function
than a lambda function, in most situations.
Lambda functions, I personally don't use them very much,
because I find them that they are a bit difficult to read
and keep track of, especially as your programmes grow.
And that's it for this video, thanks for joining me,
and I'll see you in the next one.