This tutorial will teach you about Python lambda expressions and how to use them to write anonymous functions.
Sometimes you’ll need to write a single-expression function. This function, however, must only be used once. It will also be obsolete to use the def keyword to declare that function.
Python lambda expressions are useful in this situation.
What are Lambdas in Python?
Anonymous functions can be defined using lambda expressions in Python.
Anonymous functions are those that don’t have a name. When you just need to use anonymous functions once, they’re great.
Although a lambda expression can have one or more arguments, it can only have one expression.
The syntax for lambda expressions is as follows:
lambda parameters: expression
It’s the same as the function below, but without the “anonymous” name:
def anonymous(parameters): return expression
Python lambda expression examples
Functions that accept a function example
The following defines the get full name() method, which formats the full name from the first and last names:
def get_full_name(first_name, last_name, formatter): return formatter(first_name, last_name)
The function get full name() takes three arguments:
initials (first name)
(last name) is your last name.
A function for formatting the whole name (formatter). The formatter function, on the other hand, takes two arguments: first name and last name.
The following are two functions that return a full name based on the first and last names in various formats:
def first_last(first_name, last_name): return f"{first_name} {last_name}" def last_first(first_name, last_name): return f"{last_name}, {first_name}"
And here’s how to use the first last / last first functions to use the get full name() function with the first name, last name, and first last / last first functions:
full_name = get_full_name('John', 'Doe', first_last) print(full_name) # John Doe full_name = get_full_name('John', 'Doe', last_first) print(full_name) # Doe, John
Output
John Doe Doe, John
Lambda expressions can be used instead of defining the first last and last first functions.
The first last function, for example, can be expressed using the lambda expression:
lambda first_name,last_name: f"{first_name} {last_name}"
This lambda expression takes two arguments and formats them in the order first name, space, and last name.
The following uses a lambda expression to alter the last first function to return the whole name in the format: last name, space, and first name:
lambda first_name, last_name: f"{last_name} {first_name}";
The get full name() method can be called using lambda expressions as follows:
def get_full_name(first_name, last_name, formatter): return formatter(first_name, last_name) full_name = get_full_name( 'John', 'Doe', lambda first_name, last_name: f"{first_name} {last_name}" ) print(full_name) full_name = get_full_name( 'John', 'Doe', lambda first_name, last_name: f"{last_name} {first_name}" ) print(full_name)
Output
John Doe Doe, John
 Functions that return a function example
The times() method returns a lambda expression in the following format:
def times(n): return lambda x: x * n
This example demonstrates how to use the times() function:
double = times(2)
The double is a function since the times() method returns a function. To refer to it, use parenthesis like this:
result = double(2) print(result) result = double(3) print(result)
Output
4 6
Another example of how to use the times() method is as follows:
triple = times(3) print(triple(2)) # 6 print(triple(3)) # 9
Python lambda in a loop
callables = [] for i in (1, 2, 3): callables.append(lambda: i) for f in callables: print(f())
How does it work?
First, create a list called callables.
Second, iterate from 1 to 3, adding a new lambda expression to the callables list for each iteration.
Third, invoke each function by looping through the callables.
The following is the expected output:
1 2 3
However, the following output is displayed by the programme:
Output
3 3 3
The issue is that all of their lambda expressions refer to the I variable rather than its current value. When you call the lambda expressions, the value of the variable i is 3.
To remedy this, you must bind the I variable to each lambda expression when it is formed. Using the default parameter is one option:
callables = [] for i in (1, 2, 3): callables.append(lambda a=i: a) for f in callables: print(f())
The value of an is assessed when the lambda expression is constructed in this case. As a result, the software produces the intended result.
Leave a Reply