A lambda function in Python is a small anonymous function that is defined using the “lambda” keyword. It can have any number of arguments, but can only have one expression.
A simple lambda function might look like this:
times_two = lambda number: number * 2
So this code:
print(times_two(7))
print(times_two(150))
print(times_two(-5))
would print:
>>> 14
>>> 300
>>> -10
Let’s break this syntax down:
- The function is stored in a variable called
times_two
lambda
declares that this is a lambda function (if you are familiar with normal Python functions, this is similar to how we usedef
to declare a function)number
is what we call the input we are passing intotimes_two
- We are returning
number
* 2 (with normal Python functions, we use the keywordreturn
)
Let’s write a lambda function that checks if a string is a substring of the string “This is the master string”
.
is_substring = lambda my_string: my_string in "I am learning Python"
So, the code:
print(is_substring('I'))
print(is_substring('am'))
print(is_substring('playing'))
print(is_substring('basketball'))
would print:
>>> True
>>> True
>>> False
>>> False
Lambda can also take more than one argument. For example, the following lambda function takes two arguments (x and y) and returns their sum:
sum = lambda x, y: x + y
print(sum(3, 4))
# Output: 7
We might want a function that will perform differently based on different inputs. Let’s say that we have a function check_if_A_grade
that outputs 'Got an A!'
if a grade is at least 90, and otherwise says you 'Did not get an A…'
. So, the code:
print(check_if_A_grade(91))
print(check_if_A_grade(70))
print(check_if_A_grade(20))
would print:
>>> 'Got an A!'
>>> 'Did not get an A...'
>>> 'Did not get an A...'
We can do this using an if statement in our lambda function, with syntax that looks like:
<WHAT TO RETURN IF STATEMENT IS TRUE> if <IF STATEMENT> else <WHAT TO RETURN IF STATEMENT IS FALSE>
So this is what our check_if_A_grade
function might look like:
check_if_A_grade = lambda grade: 'Got an A!' if grade >= 90 else 'Did not get an A...'
This is what this line of code does:
- Declare lambda function with an input called
grade
(lambda grade:
) - Return
'Got an A!'
if this statement is true:grade >= 90
- Otherwise, return
'Did not get an A...'
if this statement is not true:grade >= 90
As a reminder, to return different output depending on different input, we can use if
and else
inside our lambda function:
add_or_subtract = lambda input_number: input_number - 1 if input_number >= 0 else input_number + 1
The function add_or_subtract
will return your input minus 1 if your input is positive or 0, and otherwise will return your input plus 1.
Here are some examples of how it would work:
>>> add_or_subtract(0)-1
>>> add_or_subtract(8)7
>>> add_or_subtract(-4)-3
Lambda functions only work if we’re just doing a one line command. If we wanted to write something longer, we’d need a more complex function. Lambda functions are great when you need to use a function once. Because you aren’t defining a function, the reusability aspect of functions is not present with lambda functions. By saving the work of defining a function, a lambda function allows us to efficiently run an expression and produce an output for a specific task, such as defining a column in a table, or populating information in a dictionary.
Now you can make simple Python functions in one line!