Python lambda two arguments

Python Lambda Function with Two Arguments

To define a Lambda Function with two argument in Python, specify the two parameters after lambda keyword.

For example, the following is a lambda function that takes two arguments and returns their product.

Please note that the parameters are separated by comma , symbol. Also, the parameters and the function body are separated by colon : symbol.

Examples

1. Find product of two numbers using lambda function

In this example, we shall define a lambda function that takes two arguments: x, y; and returns their product.

Python Program

product = lambda x,y: x*y num1 = 5 num2 = 3 result = product(num1,num2) print(f"Product of and is .")

2. Lambda function to check if two given numbers are even

In this example, we shall define a lambda function that takes two arguments: x, y; and returns true only if both the arguments are even numbers, else it returns false.

Python Program

bothEven = lambda x,y: x % 2 == 0 and y % 2 == 0 num1 = 8 num2 = 14 if bothEven(num1,num2): print("Both the numbers are even.") else: print("Both the numbers are not even.")
Both the numbers are even.

Summary

In this tutorial of Python Lambda Function, we learned how to define a lambda function with two arguments, with the help of example programs.

Читайте также:  Instance python что это

Источник

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.

Читайте также:  Убрать рамку инпута css

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.

You may also like reading:

Источник

Оцените статью