How would the lambda functions of the exercise be rewritten in regular form?

Question

In the context of this exercise, how might the example lambda functions be rewritten using the regular form, utilizing def?

Answer

Lambda functions can usually always be written in the normal Python function structure. This post will try to rewrite the example lambda functions in the normal Python function structure, which utilizes def and consists of multiple lines, as opposed to lambda functions which are always a single line.

For the first lambda function
mylambda = lambda x: (x * 2) + 3

Rewriting this using the def structure could be as follows:

def my_function(x):
  return (x * 2) + 3

For the second example lambda function
stringlambda = lambda x: x.lower()

We can rewrite this like:

def string_function(x):
  return x.lower()

If we generalize how this is done, basically, the parameters of the function are the parameters that follow right after the keyword lambda and before the colon :. For example, for this lambda, the parameters are x and y
sumlambda = lambda x, y: x + y

The returned value of the function is just what follows the colon :,
x + y.

Written using the normal structure, this would be

def sum_function(x, y):
  return x + y
5 Likes

in this exercise I wrote:
mylambda = lambda string: string[0], string[-1] if len(string) >= 2

It didn’t work. I would like to know why.

1 Like

If you’ve got many things and as a whole they don’t work, try picking it apart, examine the parts. The first thing to get rid of is probably the lambda, leaving just the expression in your function body, much simpler already. From there there are still a lot of things you can strip away.

You could spread the expression out in a file all by itself, run the operations in it one at a time.

Or if you think it’s the lambda that is the problem, then you could get rid of the expression instead… either way, look at one thing at a time.

Type it like this to see what is happening (you’ll need add the else to make the in-line conditional work):

string = "abc"
print(lambda string: string[0], string[-1] if len(string) >= 2 else string[0])

'''Output:
<function <lambda> at 0x03A87390> c
'''

The interpreter thinks it is dealing with a two-item tuple consisting of a lambda having a variable, string, and an in-line conditional expression, like this:

print((lambda string: string[0]), (string[-1] if len(string) >= 2 else string[0]))

… and imports string from the outer scope to fulfill the conditional.


Tuples are indeed defined by commas, not parentheses, but parentheses must occasionaly be used, and you have discovered one of those times:

string = "abc"
my_lambda = lambda string: (string[0], string[-1]) if len(string) >= 2 else string[0]

print(my_lambda(string))
'''Output:
('a', 'c')
'''
2 Likes

Can lambda function return multiple values?

Yes It can return multiple values.
ex_lambda=lambda a,b: (a+1,b+1)

You can concatenate a string using the ‘+’ operation.
mystring= lambda x: x[0]+x[-1] if len(x)>2 else ''

2 Likes

The regular def function will work as follows:

def name_access(string):
name1 = string[0] + string[-1]
if len(string) >= 2:
return name1
string = “Charlton”
initials = name_access(string)
print(initials)

Output: Cn