Assignment Inside Lambda Expression in Python

Is it possible to assign variables a value in a lambda function?

Yes, with the new := "walrus" assignment operator, you can make assignments in an expression, including in lambdas. Note that this is a new feature requiring Python 3.8 or later.

lambda x, y: (avg:=x+y)

There are ways to simulate this operator on older versions of Python as well, but := is the proper way to do it now.

How to perform an assignment inside a lambda function

You can't use assignment in an expression, it is a statement. A lambda can only contain one expression, and statements are not included.

You can assign to the map though, by using the operator.setitem() function instead:

import operator

lambda map: operator.setitem(map, 'x', 'y')

Can you assign variables in a lambda?

Nope, you can't. Only expressions allowed in lambda:

lambda_expr        ::=  "lambda" [parameter_list]: expression
lambda_expr_nocond ::= "lambda" [parameter_list]: expression_nocond

You could, however, define a second lambda inside the lambda and immediately call it with the parameter you want. (Whether that's really better might be another question.)

>>> a = lambda n: ((3+2*n), n*(3+2*n))  # for reference, with repetition
>>> a(42)
(87, 3654)
>>> a2 = lambda n: (lambda b: (b, n*b))(3+2*n) # lambda inside lambda
>>> a2(42)
(87, 3654)
>>> a3 = lambda n: (lambda b=3+2*n: (b, n*b))() # using default parameter
>>> a3(42)
(87, 3654)

Of course, both the outer and the inner lambda can have more than one parameter, i.e. you can define multiple "variables" at once. The benefit of this approach over, e.g., defining a second lambda outside of the first is, that you can still also use the original parameters (not possible if you invoked a with b pre-calculated) and you have to do the calculation for b only once (other than repeatedly invoking a function for the calculation of b within a).


Also, inspired by the top answer to the linked question, you could also define one or more variables as part of a list comprehension or generator within the lambda, and then get the next (first and only) result from that generator or list:

>>> a4 = lambda n: next((b, n*b) for b in [3+2*n])
>>> a4(42)
(87, 3654)

However, I think the intent behind the lambda-in-a-lambda is a bit clearer. Finally, keep in mind that instead of a one-line lambda, you could also just use a much clearer three-line def statement...


Also, starting with Python 3.8, there will be assignment expressions, which should make it possible to write something like this. (Tested with Python 3.8.10.)

>>> a5 = lambda n: ((b := 3+2*n), n*b)

lambdas can't have assignment statements - so why is foo = lambda x: x * 2 legal?

You can't have assignments inside the "lambda" function, but the lambda itself can be used in assignments.

So you can't say something like lambda x: y = x*2; return y, but you can say foo = lambda x: x*2

Lambda expressions in Python

They can be split across multiple lines by the same rule that any expression can be split across multiple lines. You can use backslash \ to prevent a linebreak ending the current statement, or use the fact that linebreaks are permitted within the various forms of brackets: (), [], {}.

However, a lambda expression is just that, an expression. It cannot contain assignment statements (or any other statements).

The precise details are defined by the Python grammar.



Related Topics



Leave a reply



Submit