Jump to content

Lambda function and list: Can't undersatnd the output of lambda functio and its relationship with list


zak100

Recommended Posts

Hi,

I can't understand the code, is it creating five lambda function definition with in the list, I have got the following code;

 

print("list3")
fun_list3 = [lambda e: e+i for i in range(5)]
#The lambda function returns e+i for i in range(5), Note e is the argument of lambda function
print([f(10) for f in fun_list3])

The output is:

I can’t understand the output of the function, its printing:

 

Quote

[14. 14, 14, 14, 14]

 

 

From print statement we are passing 10 to the lambda function argument, lambda function evaluates “e+i” so its output is 10 first time, and second time, it should be 11, and 3rd time 12 and so on, but I can’t understand this output.

 

Somebody please guide me.

 

Zulfi.

Link to comment
Share on other sites

Change code to

print("list3")
fun_list3 = [lambda e,i=i: e+i for i in range(5)]
#The lambda function returns e+i for i in range(5), Note e is the argument of lambda function
print([f(10) for f in fun_list3])

It is making yet another argument for lambda function which is by default initialized to i (which is taken from for loop).

or if you want it to be cleaner:

print("list3")
fun_list3 = [lambda e,f=i: e+f for i in range(5)]
#The lambda function returns e+i for i in range(5), Note e is the argument of lambda function
print([f(10) for f in fun_list3])

It will give you expected result:

Quote

[10, 11, 12, 13, 14]

Edited by Sensei
Link to comment
Share on other sites

fun_list3 = [lambda e: e + i for i in range(5)]

The variable i is outside the scope of the lambda function. Let's rewrite the above to make this clearer.

fun_list3 = []
for i in range(5):
    fun_list3.append(lambda e: e + i)

This will produce the same list you wrote but is more explicit about what's going on. The variable i in your lambda function is referencing the i variable from the for loop. It's not doing what you think, which is inserting the value of i over each iteration. The i in your lambda function and the i in the for loop are pointing to the same place in memory. You can see this by doing the following:

Python 3.8.5 (default, Sep  5 2020, 10:50:12)
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> fun_list3 = []
>>> for i in range(5):
...     fun_list3.append(lambda e: e + i)
...
>>> print([f(10) for f in fun_list3])
[14, 14, 14, 14, 14]
>>> i = 555
>>> print([f(10) for f in fun_list3])
[565, 565, 565, 565, 565]
>>>

 

You can do what Sensei said above, but that will introduce an optional argument for each of the lambda functions in the list. For example,

Python 3.8.5 (default, Sep  5 2020, 10:50:12)
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> fun_list3 = [lambda e,f=i: e+f for i in range(5)]
>>> print([f(10) for f in fun_list3])
[10, 11, 12, 13, 14]
>>> print([f(10, 555) for f in fun_list3])
[565, 565, 565, 565, 565]
>>>

 

I personally would do the following which is have a lambda function that takes i as its argument and  returns another lambda function.

 

fun_list3 = [(lambda f: lambda e: e + f)(i) for i in range(5)]

 

Python 3.8.5 (default, Sep  5 2020, 10:50:12)
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> fun_list3 = [(lambda f: lambda e: e + f)(i) for i in range(5)]
>>> print([f(10) for f in fun_list3])
[10, 11, 12, 13, 14]
>>> print([f(10, 555) for f in fun_list3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
TypeError: <lambda>() takes 1 positional argument but 2 were given
>>>

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.