Kodeclik Blog

When would you need a nested lambda in Python?

Remember that lambda functions are unnamed, small, functions that are “use and throw” - defined at a point in a program and used there, and not re-used anywhere. They are typically defined in terms of a single expression and return values based on input arguments. A nested lambda is a lambda inside a lambda and lets see how and when to use it!

Writing without lambdas

Here is a simple Python program that actually uses named functions. First we write a square function that computes the square of a number. Then we write a function that applies the function to every element of a list and thus generate a list of squares:
The output is:
Note that the squares() function uses square() inside it. If the only place you intend to compute squares is for the list [1,2,3] then creating these named functions might be an overkill. We can replace these named functions with lambdas. Here is how that works.

Writing a nested lambda function

Let us rewrite the above program to only use lambdas. First, we will replace the outer function with a lambda, like so:
We have commented out the earlier lines so you can see the structure. Note that because the squares() function is no longer available, it is replaced with a lambda in the print function. That lambda function essentially replicates the logic of the original squares() function using an argument called “x”. If we run this program, we will get the same output:
Now let us replace the inner square() function with a lambda as well. Here is the updated program:
You can see that the square() function is gone (commented out) and in its place is a lambda function that takes “y” as an argument and returns “y*y”. The output is (still):
In this example the inner and outer lambdas are connected in a purely functional manner. In general, the inner lambda can access variables from the outer lambda. In other words, the function that currently uses “y” as input can also refer to “x” in its body.

Nested lambda function to multiply two numbers

Here is a very simple nested lambda approach to multiply two numbers:
The output is:
Note that the outer lambda has argument “x” and the inner lambda has argument “y” but the inner lambda refers to the outer lambda’s argument. Because these are lambda functions each taking one argument, we must pass the arguments one by one, which is what we have done above. We first pass (3) as an argument (which becomes the value of “x”). That returns a lambda (the inner lambda) that simply multiples a number by 3. To this lambda, we are passing 5 as an argument and thus the output is 15.
It is important we pass arguments in the order intended and only as many arguments as intended. If we did something like this:
We will get an error:
This is because remember that each lambda is expecting only one argument (the arguments are listed before the “:” colon).
So we cannot pass arguments in a list. We have to pass them as individual arguments in the order written. But you can pass partial arguments. In other words for a nested lambda that takes one argument to the outer function and one to the inner lambda, it is okay to pass just one argument. Why? What would that do? Lets us try it!

Passing a single argument to a nested lambda

If we do something like this:
The output will be (something like):
In other words, Python returns a function because that is what the code above is supposed to do: it returns the inner lambda! We can potentially take the inner lambda, assign it to a variable and use that variable as a function and continue from that point:
In the first line we have a nested lambda function. But we passed only one argument to it, so x is assigned the value of 6. Thus, this nested lambda evaluates to: (lambda y: 6*y). That function is now henceforth referred to as “mypartialfunction”. Note that the semantics of mypartial function is that it simply takes one argument (namely, y) and returns 6 times that argument. Thus, in the second line we multiply 6*5 to obtain:
To summarize, just like lambdas are “use and throw” functions, nested lambdas are a little bit more complex use and throw functions. They enable you to create more functions on the spot without defining them and enable you to keep your code concise.
If you liked learning about nested lambdas, you should next investigate if you can have a multiline lambda function in Python. These concepts appear closely related but quite different!
For more Python content, checkout the math.ceil() and math.floor() functions! Also learn about the math domain error in Python and how to fix it!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2024. All rights reserved.