An overview and examples of using LAMBDA functions in Python.
A LAMBDA function is a small function written on one line of code.
Links to topics on this page
- Components of a lambda function
- Nest a lambda function inside a named function
- Lambda example – create a new column by splitting another column’s values
- Python Lambda example – filter a list based on values
- Pros and cons of using lambda functions
In Python, a lambda function looks like this:
lambda x : x + x
- It’s referred to as an ‘anonymous function’ because it is not ‘named’, i.e. not assigned to a named object/variable.
- It’s a short term function because it’s only used once where it’s defined, and can’t be called elsewhere in the script or from another script.
- A lambda function can take multiple arguments but only one expression.
The components of a lambda function are:
- the word
- its argument(s), e.g.
- a colon
- the expression (operation) to perform on the arguments. This is equivalent to the ‘body’ of a function.
lambda x : x + x
… is equivalent to the defined
adder() function below:
return x + x
Nest a lambda function inside a named function
Lambda functions are often used within the scope of a larger function. We can nest a lambda function inside a named function.
Below we transform our
adder() function to include an input
y and the addition expression (
return lambda y: x + y
- The argument of
- The argument of the lambda function is
y. The function is also taking the argument x from the parent function adder().
- The lambda expression is
x + y.
- The output of
adder()is the result of the lambda operation.
Lambda example – create a new column in Python by splitting another column’s values
In this example below we create a new column taking the first word from a string in another column, e.g. extracting a person’s first name from their full name. In this example:
dfis our dataframe
Full_nameis the name of the existing column
First_nameis the name of the new column.
df[“First_name”] = df [“Full_name”].apply(lambda x: x.split(” “))
- The nested lambda function is:
lambda x: x.split(" "))
xstring is split into words separated by a space
" "and the first element
of the resulting output is selected.
applyfunction is used to iterate through the rows of the table
Python Lambda – filter a list based on values
In the example below, the lambda function is used to iterate through the list and return any values that are greater than 5. The output is assigned to the new variable
my_list = [15, -3, 5, 0, 6, 22]
new_list = list(filter(lambda x: x > 5, my_list))
[15, 6, 22]
Pros and cons of using Lambda functions
What’s good about Lambda functions
- Good for creating simple logical operations.
- The conciseness of the lambda function structure can make code easy to read.
- If you just need to run a function once in your code, e.g. a quick data transformation, or string split, lambda functions are just the job..
What’s not so good about Lambda functions
- You can only carry out one operation or expression in a lambda function, so the function cannot involve complex logic.
- Lambda functions can’t include default values for arguments.
- Lambda functions can’t span more than one line.
- Keep it simple – if it’s not clear what the function is doing use a named function instead.
- Well written code will include docstrings – text used to document what a function, module, class or method does – which can’t be added to lambda functions. See more about docstrings here: https://peps.python.org/pep-0257/