Say I need to build n lambda functions, and put them into a list:
n = 6
lamd_list = [lambda x: x * i for i in range(n)]
Here x
is input of the lambda functions, and the i
is intended to be fixed to 0~5 in the list. However, when I run:
lamb_list[0](3)
The output would be 15 (3 * 5) instead of 0 (0 * 5). How can I modify the code to make it do as I intended?
Note that here the n
is a variable (not fixed), so I can't do the loop manually. Also, I don't want i
as an input to the lambda function.