Python - How To Pass Multiple Arguments in Lambda Functions in Python

ID : 239

viewed : 216

Tags : PythonPython FunctionPython Lambda Function

vote vote

99

The lambda forms or lambda expressions are anonymous functions in Python. They are inline functions that can be created using the reserved lambda keyword in Python.

This article will talk about the lambda functions in Python and learn how to deal with multiple arguments in them.

Lambda Functions in Python

A lambda function comprises three parts: the lambda keyword, the parameters or the bound variables, and the function body. The function body can only have a single Python expression since these functions are inline.

These functions can not only be invoked immediately but also used like other regular Python functions.

Lambda functions have the following syntax:

lambda <parameters comma seperated>: expression 

Note that the expression in the function body should return some value. If the expression does not return any value, the result from a lambda function will be a None value.

For inline invocation, we surround the lambda function within parentheses and place the values for the arguments next to it enclosed within parentheses.

Below is the syntax for this:

(lambda <parameters comma seperated>: expression) (<parameters comma seperated>) 

To understand these lambda functions, let us create a lambda function that multiplies two numbers. As we discussed that these functions could be invoked immediately and used as regular Python functions, the examples will include both versions of the lambda functions.

Refer to the following code for the multiplication example:

# Regular function calls multiply = lambda a, b : a * b print(multiply(1, 2)) print(multiply(10, 5)) print(multiply(10.5, 9.3)) print(multiply(0.945, -5.645)) print(multiply(1000e9, 0), end = "\n\n")  # Inline invocation print((lambda a, b : a * b) (1.1, 1.2)) print((lambda a, b : a * b) (10, 5)) print((lambda a, b : a * b) (10.5, 9.3)) print((lambda a, b : a * b) (0.945, -5.645)) print((lambda a, b : a * b) (1000e9, 0)) 

Output:

2 50 97.65 -5.334524999999999 0.0  1.32 50 97.65 -5.334524999999999 0.0 

To make it even more precise, let’s consider three more examples where we will filter odd values from a list of numbers, compute squares of elements of a list, and compute cube roots of elements of a list.

Refer to the following Python code for the first example:

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y = [22, 44, 66, 88, 110] z = [78, 9797, 97, 985, 75473, 2845, 74, 9964, 652, 124, 0, 6747]  # Regular function calls filter_odd = lambda a : a % 2 != 0 print(list(filter(filter_odd, x))) print(list(filter(filter_odd, y))) print(list(filter(filter_odd, z)), end = "\n\n")  # Inline invocation print((lambda array : list(filter(lambda a : a % 2 != 0, array))) (x)) print((lambda array : list(filter(lambda a : a % 2 != 0, array))) (y)) print((lambda array : list(filter(lambda a : a % 2 != 0, array))) (z)) 

Output:

[1, 3, 5, 7, 9] [] [9797, 97, 985, 75473, 2845, 6747]  [1, 3, 5, 7, 9] [] [9797, 97, 985, 75473, 2845, 6747] 

Refer to the following Python code snippet for the second example:

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y = [22, 44, 66, 88, 110] z = [78, 9797, 97, 985, 75473, 2845, 74, 9964, 652, 124, 0, 6747]  # Regular function calls square = lambda a : a ** 2 print(list(map(square, x))) print(list(map(square, y))) print(list(map(square, z)), end = "\n\n")  # Inline invocation print((lambda array : list(map(lambda a : a ** 2, array))) (x)) print((lambda array : list(map(lambda a : a ** 2, array))) (y)) print((lambda array : list(map(lambda a : a ** 2, array))) (z)) 

Output:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100] [484, 1936, 4356, 7744, 12100] [6084, 95981209, 9409, 970225, 5696173729, 8094025, 5476, 99281296, 425104, 15376, 0, 45522009]  [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] [484, 1936, 4356, 7744, 12100] [6084, 95981209, 9409, 970225, 5696173729, 8094025, 5476, 99281296, 425104, 15376, 0, 45522009] 

And, refer to the following Python code snippet for the third example:

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y = [22, 44, 66, 88, 110] z = [78, 9797, 97, 985, 75473, 2845, 74, 9964, 652, 124, 0, 6747]  # Regular function calls square = lambda a : a ** (1 / 3) print(list(map(square, x))) print(list(map(square, y))) print(list(map(square, z)), end = "\n\n")  # Inline invocation print((lambda array : list(map(lambda a : a ** (1 / 3), array))) (x)) print((lambda array : list(map(lambda a : a ** (1 / 3), array))) (y)) print((lambda array : list(map(lambda a : a ** (1 / 3), array))) (z)) 

Output:

[1.0, 1.2599210498948732, 1.4422495703074083, 1.5874010519681994, 1.7099759466766968, 1.8171205928321397, 1.912931182772389, 2.0, 2.080083823051904, 2.154434690031884] [2.802039330655387, 3.530348335326063, 4.04124002062219, 4.4479601811386305, 4.791419857062784] [4.272658681697917, 21.397565740522946, 4.594700892207039, 9.949747895601458, 42.2601016892268, 14.169703309060843, 4.198336453808407, 21.518462597981888, 8.671266460286839, 4.986630952238645, 0.0, 18.896015508976504]  [1.0, 1.2599210498948732, 1.4422495703074083, 1.5874010519681994, 1.7099759466766968, 1.8171205928321397, 1.912931182772389, 2.0, 2.080083823051904, 2.154434690031884] [2.802039330655387, 3.530348335326063, 4.04124002062219, 4.4479601811386305, 4.791419857062784] [4.272658681697917, 21.397565740522946, 4.594700892207039, 9.949747895601458, 42.2601016892268, 14.169703309060843, 4.198336453808407, 21.518462597981888, 8.671266460286839, 4.986630952238645, 0.0, 18.896015508976504] 

Pass Multiple Arguments in Lambda Functions

To pass multiple arguments in the lambda function, we must mention all the parameters separated by commas. Let us understand this with an example.

We will create a lambda function that takes three parameters; a list and two integers. The lambda function will add the first integer and subtract the second integer from each list element.

Refer to the following Python code for this:

x1 = [ 1, 8, 27, 64, 125, 216, 343, 512] x2 = 5 x3 = 6 y1 = [11, 22, 33, 44, 55, 66, 77, 88, 99] y2 = 4 y3 = 1 z1 = [78, 9797, 97, 985, 75473, 2845, 74] z2 = 99 z3 = 99  # Regular function calls modify = lambda a, b, c : [x + b - c for x in a] print(modify(x1, x2, x3)) print(modify(y1, y2, y3)) print(modify(z1, z2, z3), end = "\n\n")  # Inline invocation print((lambda a, b, c : [x + b - c for x in a]) (x1, x2, x3)) print((lambda a, b, c : [x + b - c for x in a]) (y1, y2, y3)) print((lambda a, b, c : [x + b - c for x in a]) (z1, z2, z3)) 

Output:

[0, 7, 26, 63, 124, 215, 342, 511] [14, 25, 36, 47, 58, 69, 80, 91, 102] [78, 9797, 97, 985, 75473, 2845, 74]  [0, 7, 26, 63, 124, 215, 342, 511] [14, 25, 36, 47, 58, 69, 80, 91, 102] [78, 9797, 97, 985, 75473, 2845, 74] 

  • Related HOW TO?