JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Passing multiple parameters in Lambda function in Python

Author:JIYIK Last Updated:2025/05/05 Views:

Lambda forms or lambda expressions are anonymous functions in Python. They are lambdainline functions that can be created using the reserved keywords in Python.

In this article, we will discuss lambda functions in Python and learn how to handle multiple parameters in them.


Lambda functions in Python

A lambda function consists of three parts: lambdakeywords, arguments or bound variables, and the function body. The function body can only have one Python expression because these functions are inline.

Not only are these functions immediately callable, they can also be used like any other regular Python function.

A Lambda function has 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 of the lambda function will be Nonethe value.

For an inline call, we enclose the lambda function in parentheses and place the parameter values ​​next to it in parentheses.

Following is the syntax for this operation:

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

To understand these lambda functions, let's create a lambda function that multiplies two numbers. As we discussed, these functions can be immediately called and used as regular Python functions, so the examples will include two versions of the lambda function.

Refer to the following code for a multiplication example:

# Regular function calls
def multiply(a, b):
    return 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 be more precise, let's consider three more examples where we will filter out odd values ​​from a list of numbers, calculate the square of list elements, and calculate the cube roots of list elements.

For the first example, please refer to the following Python code:

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


def filter_odd(a):
    return 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]

For the second example, see the following Python code snippet:

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


def square(a):
    return 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]

Also, see 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


def square(a):
    return 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]

Passing multiple parameters in Lambda functions

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

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

To do this, see the following Python code:

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


def modify(a, b, c):
    return [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]

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Convert a list to a set in Python

Publish Date:2025/05/08 Views:144 Category:Python

This tutorial demonstrates how to convert a list to a set in Python. Difference between List and Set Lists and sets are standard Python data types that store values ​​in sequence. Python list stores comma separated values ​​between

Remove the first element from a list in Python

Publish Date:2025/05/08 Views:172 Category:Python

This tutorial will discuss different ways on how to remove the first element from a list. pop() Remove the first element from a list using the pop() Method can remove an element from a specific index. We have to specify the index from which

Convert a list to lowercase in Python

Publish Date:2025/05/08 Views:112 Category:Python

Lists can be used to store multiple items in a single variable. In Python, we can create a list of strings by enclosing the different elements in the list in single or double quotes. This tutorial demonstrates how to convert a list of strin

Remove all occurrences of an element from a Python list

Publish Date:2025/05/08 Views:69 Category:Python

In Python, lists allow the same element to appear multiple times. Even though the value of an element may be the same as other elements, each element will have a different index. Using these index numbers, you can easily access any element

Convert hex to bytes in Python

Publish Date:2025/05/08 Views:101 Category:Python

Hexadecimal, often abbreviated to hex, uses 16 symbols (0-9, a-f) to represent values, in contrast to the decimal system's 10. For example, 1000 in decimal is 3E8 in hexadecimal. Being proficient in dealing with hexadecimal is essential for

b in front of string in Python

Publish Date:2025/05/08 Views:53 Category:Python

This tutorial will discuss the statement in Python b" . b" Using the statement in Python b" The notation is used to specify strings in Python bytes . In contrast to regular strings with ASCII characters, bytes a string is an array of byte v

How to Convert Integer to Binary in Python

Publish Date:2025/05/08 Views:130 Category:Python

This tutorial explains how to convert an integer to binary in Python. This tutorial also lists some sample codes to illustrate different ways of converting from int to binary in Python. bin() Convert Int to Binary in Python using In Python,

How to convert an integer to bytes

Publish Date:2025/05/08 Views:77 Category:Python

Converting an integer int to a byte bytes is the inverse of bytes converting a byte to an integer . Most of the to methods int described in this article are the inverse of the to methods. int bytes bytes int Generic method for converting in

How to convert bytes to int in Python

Publish Date:2025/05/08 Views:111 Category:Python

Bytes The data type has a numerical range of 0~255 (0x00~0xFF). A byte has 8 bits of data, that's why its maximum value is 0xFF. In some cases, you need to convert a byte or byte array to an integer for further data processing. Let's see ho

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial