+ 1
Is it possible to write this code line with lambda function ? If it is how so?
11 Answers
0
Anonymous function
lambda x: x+5
0
lambda functions have nothing to do with lambda calculus.
0
Okay, I've changed the question. I read that lambda functions get their name from lambda calculus.That's why I asked it in that way.
0
Mirielleđ¶ [Inactive] I couldn't understand how your expression work. Probably because I have a misconception about lambda functions. How come your expression work and
Print((lambda x: x+5)([1,3,5])) doesn't?
0
Baran Aldemir itâs the for loop. You need to iterate through the values and add 5 to each one, and for that you need a for loop (list comp).
0
Rora So, lambda has to take values one by one. It can not deal with a list at once?
0
Mirielleđ¶ [Inactive] Have you you tried it? Because I get the error below.
TypeError: can only concatenate list (not "int") to list
0
Mirielleđ¶ [Inactive] I think I got it. If the lambda function returns an int, argument has to be int as well. If the lambda function returns a list, argument has to be list. Otherwise it can not concatenate. Because similar code to your previous code
print((lambda x: [i+5] for i in x)([1,2,3])) works. Am I right?