Jump to content

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

Featured Replies

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.

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

  • Author

Hi,

I am getting the answer:

[14. 14, 14, 14, 14]

 

instead of :

[10, 11, 12, 13, 14]

 

This is the confusion. Somebody please guide me why the output is: [14. 14, 14, 14, 14]

 

Zulfi.

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
>>>

 

Archived

This topic is now archived and is closed to further replies.

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.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.