I do not understand why my code is not working for the lambda function, but it works in the other format. Some please help me understand.
The exercise question is as follows, " Create a lambda function
mylambda that returns the first and last letters of a string, assuming the string is at least 2 characters long."
mylambda = lambda x: x+x[len(x)-1] # This worked, but I don’t understand why. What does “x[len(x)-1]” even mean? I have never seen code formatted this way.
mylambda = lambda y: y + y[-1] if y >= 2 #didn’t work
if len(n) >= 2:
return n + n[-1]