- Python Lambda Function
- Syntax of Lambda Function
- Examples
- 1. Find square of a number using Lambda function
- 2. How to pass multiple arguments to lambda function
- 3. Lambda function with no arguments
- 4. Recursive lambda function
- 5. Return lambda function
- Tutorials on Lambda Function
- Summary
- Python Lambda with Multiple Arguments
- 1. Python Lambda with Two Arguments
- 3. Using Multiple Iterables
- 4. Using String List as Multiple Arguments
- Conclusion
- Related Articles
- You may also like reading:
Python Lambda Function
Python Lambda function is a single line function whose body is just an expression and can accept multiple arguments.
In this tutorial, you shall first learn the basics of lambda function and secondly some interesting hacks that we can do with these lambda functions.
Syntax of Lambda Function
The syntax of a Lambda Function is as shown in the following.
lambda arguments : expression
You can assign this lambda function to a variable. Something like in below code snippet.
x = lambda arguments : expression
And you can call the lambda function with the variable name x. Following is an example code snippet to call the lambda function.
The arguments are passed just like a regular function’s arguments, but the function name being the variable we assigned this lambda function to.
When you call a lambda function, the expression is evaluated and the evaluated value is returned. So, you have the arguments, body and return statement, everything in just a line. So very concise.
Examples
1. Find square of a number using Lambda function
Following is a simple lambda function, that takes a number and returns the square of it.
Python Program
#lambda function square = lambda a: a*a #call lambda function result = square(6) print(result)
2. How to pass multiple arguments to lambda function
We already mentioned that a Python Lambda function can accept multiple arguments. In the following example, we define a lambda function with two arguments, say a and b, and we shall multiplication of a and b.
Python Program
#lambda function mul = lambda a,b: a*b #call lambda function result = mul(5,3) print(result)
3. Lambda function with no arguments
Yeah! You can define a lambda function with no arguments at all. If there were to be requirement that you create a lambda function with no arguments, you can.
In the following example, we define a lambda function with no arguments, and returns 6 when called.
Python Program
#lambda function six = lambda : 6 #call lambda function result = six() print(result)
4. Recursive lambda function
You can design a recursion function using lambda function. According to the syntax of Python Lambda function, all that you need is an expression that evaluates to a single value to return. So, if you can build a recursive function satisfying this criteria, you have a recursive lambda function.
In the following example, we defined a lambda function that can calculate the factorial of a given number, using recursion.
Python Program
#recursive lambda function factorial = lambda a: a*factorial(a-1) if (a>1) else 1 #call lambda function result = factorial(5) print(result)
5. Return lambda function
Lambda Function is just like a variable. So, you can return a lambda function from another function.
Python Program
import math #function returning lambda function def myfunc(n): return lambda a : math.pow(a, n) #lambda functions square = myfunc(2) #square = lambda a : math.pow(a, 2) cube = myfunc(3) #cube = = lambda a : math.pow(a, 3) squareroot = myfunc(0.5) #squareroot = lambda a : math.pow(a, 0.5) print(square(3)) print(cube(3)) print(squareroot(3))
Tutorials on Lambda Function
The following tutorials cover some use cases with lambda function, to understand them better in different scenarios.
Summary
Concluding this tutorial of Python Examples, we learned how to define a lambda function, and use it in your program constructively, with the help of example programs.
Python Lambda with Multiple Arguments
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. Here is the syntax of the 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.
Here is a simple example of lambda.
# Lambda example with single argument square = lambda x: x * x print(square(4)) # Output: # 16
1. 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.
# Lambda example with two arguments add = lambda x, y : x + y print(add(10, 20)) #Output: #30
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.
result = (lambda x, y : x + y)(10,20) print(result)
3. 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.
4. 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']
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.