New course! Every coder should learn Generative AI!
Try a free lesson+ 1
Why is the output 222? A question from challenge
2 Answers
+ 3
https://www.sololearn.com/discuss/1265773/?ref=app
https://www.sololearn.com/discuss/1267956/?ref=app
0
As already said in suggested threads, the output should be the result of three lambdas, invoked with the same argument 1, and factorized by a parameter i which is expected different for each of the three lambdas, respectively 0 1 2. So the expected result should be 012 instead of 222.
Why this?
Because a list is evaluated only when it is used, and not when it is being built: the parameter i, at the moment of the list use, has the same final value 2 for each of the three lambdas.
To better understand this, I tried to code some examples using simpler lambdas without arguments.
Examples (1) and (2) show this unexpected - maybe unwanted - behaviour.
All the other examples are possible alternatives to obtain the normal behaviour, including generator expressions and generator functions.
https://code.sololearn.com/cXvSlXFaDG8b/?ref=app