You can place a for loop inside a lambda function. how? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

You can place a for loop inside a lambda function. how?

f = lambda x : x for... ?

8th Mar 2017, 1:57 AM
Javier I. Rivera R.
Javier I. Rivera R. - avatar
3 Answers
+ 4
f=lambda x: [i**2 for i in range(x)] print(f(5))
8th Mar 2017, 6:19 AM
Álvaro
+ 2
Well, the computer says no: https://docs.python.org/2/reference/expressions.html#lambda ...but I guess you could encapsulate your loop into a function, and then use 'map' with this function... although that would miss the point of defining a lambda expression IMHO.
8th Mar 2017, 12:09 PM
Álvaro
+ 1
Thank Alvaro.. there will be another way without using compression v list ?
8th Mar 2017, 10:24 AM
Javier I. Rivera R.
Javier I. Rivera R. - avatar