Python lambda can be used with multiple arguments and these arguments are used in evaluating an expression to return a single value. A Python lambda function is used to execute an anonymous function, an anonymous meaning function without a name. This function can take any number of arguments, but can only have one expression and they can be used wherever function objects are required.
1. Quick Examples of Lambda with Multiple Arguments
If you are in a hurry, below are some quick examples of Python lambda with multiple arguments.
# Quick examples of lambda with multiple arguments
# Example 1: Lambda with single argument
square = lambda x: x * x
result = square(4)
# Example 2: Lambda example with two arguments
add = lambda x, y : x + y
result = add(10, 20)
# Example 3: Using the lambda function
result = (lambda x, y : x + y)(10,20)
# Example 4: Create lambda function that takes two arguments
numbers1 = [2, 4, 5, 6, 3]
numbers2 = [1, 3, 2, 2, 4]
add_fun = lambda x, y: x+y
add_result = list(map(add_fun, numbers1, numbers2))
# Example 5: Use lower() function
myStrings1 = ["A","B","C","D"]
myStrings2 = ["a","b","c","d"]
lower_result = list(map(lambda x,y: x + y, myStrings1, myStrings2))
2. Syntax of Lambda
Following is the syntax of lambda
# Syntax of lambda
lambda argument, [argument, argument]: expression
Note that the expression in the lambda function body should return some value. If the expression does not return any value, the result from a lambda will be a None
value.
3. Lambda with Single Argument
You can use a lambda function with a single argument. It defines a lambda function named square
that takes one argument (x
) and returns the square of that argument. In this case, when you call square(4)
, it evaluates to 4*4
, resulting in 16
, which is then printed.
# Lambda example with single argument
square = lambda x: x * x
# Using the lambda function
result = square(4)
print("Lambda with single argument:",result)
Yields below output.
![python lambda multiple arguments](https://i0.wp.com/sparkbyexamples.com/wp-content/uploads/2023/11/image-278.png?resize=301%2C45&ssl=1)
4. Python Lambda with Two Arguments
You can create a lambda function with multiple arguments in Python, let’s see an example with two arguments, add them and return the result. Here, I have created a add variable and assigned a lambda function to it.
Regardless of how many arguments you use with lambda, it always returns a single value. However, you can use the lambda function with map() to work on the iterable and get the iterable as a return type.
In this example, the lambda function named add
takes two arguments, x
and y
, and returns their sum. You can replace the function body with any expression or operation that you want to perform with these two arguments.
# Lambda example with two arguments
add = lambda x, y : x + y
result = add(10, 20)
print("Lambda with two argument:",result)
Yields below output.
![python lambda multiple arguments](https://i0.wp.com/sparkbyexamples.com/wp-content/uploads/2023/11/image-279.png?resize=270%2C39&ssl=1)
Alternatively, you can also write the statement as follows. This is called inline execution. For inline invocation, we surround the lambda function with parentheses and place the values for the arguments next to it enclosed within parentheses.
# Using the lambda function
result = (lambda x, y : x + y)(10,20)
print(result)
5. Using Multiple Iterables
Let’s create a lambda function with multiple arguments, and take the values for these arguments from the list, To pass values to these arguments you need to use two iterables.
# Create two lists with numbers
numbers1 = [2, 4, 5, 6, 3]
numbers2 = [1, 3, 2, 2, 4]
# Create lambda function that takes two arguments
add_fun = lambda x, y: x+y
# Use lambda with map() function
add_result = list(map(add_fun, numbers1, numbers2))
print(add_result)
# Output:
# [3, 7, 7, 8, 7]
Here, the Python lambda takes multiple arguments x
, y
and adds their values. The map() function applies this lambda function to each item of the numbers1
& numbers2
lists, and the result is a new iterator containing the sum of both numbers element-wise.
6. Using String List as Multiple Arguments
So far you have seen calling the custom functions, now let’s use the lower()
function with lambda and map(). Here, I convert the list of string values to lowercase.
# Create string list
myStrings1 = ["A","B","C","D"]
myStrings2 = ["a","b","c","d"]
print("myStrings1:",myStrings1)
print("myStrings2:",myStrings2)
# Use lower() function
lower_result = list(map(lambda x,y: x + y, myStrings1, myStrings2))
print("Result:",lower_result)
# Output:
# myStrings1: ['A', 'B', 'C', 'D']
# myStrings2: ['a', 'b', 'c', 'd']
# Result: ['Aa', 'Bb', 'Cc', 'Dd']
Frequently Asked Questions On Python Lambda Multiple Arguments
Creating a lambda function with three or more arguments in Python is similar to creating one with two arguments. You simply list the parameters separated by commas and then provide the expression.
Lambda functions in Python cannot have default values for their parameters. Lambda functions are limited to expressing simple anonymous functions with a single expression.
If you have a list of arguments and you want to use them with a lambda function, you can unpack the list using the *
operator.
You can use lambda functions with functions like map
or filter
that expect a function and an iterable as arguments. When using lambda functions with map
and filter
, the lambda function is applied to each element of the iterable.
Lambda functions in Python cannot have variable keyword arguments. Lambda functions are restricted to a simple syntax with a single expression and a limited set of features. Specifically, lambda functions can only take positional arguments, and they do not support default values, variable positional arguments (*args
), or variable keyword arguments (**kwargs
).
You can pass a lambda function as an argument to another function in the same way you pass any other function. Lambda functions are first-class citizens in Python, meaning you can treat them like any other object, including passing them as arguments to functions.
Conclusion
In this article, you have learned how to use Python lambda with multiple arguments with examples. Also learned to use multiple iterable arguments with map()
and lambda function.
Related Articles
- Sort using Lambda in Python
- Python Lambda using if else
- Python map() with Lambda Function
- Using filter() with Lambda in Python
- Python Sort List Alphabetically
- How to Sort List of Strings in Python
- How to Sort Dictionary by Value in Python
- Sort using Lambda in Python
- Python Lambda using if else
- Python Lambda with Multiple Arguments
- Python map() with Multiple Arguments
- Relative Imports in Python 3