-2

I am working on some python exercises. I understand there are more efficient ways of solving this problem, however, I am trying to get a better understanding of what the question is asking.

Q: Use a map and lambda to pass multiple functions to a single value.

It is important to note here that I have not assigned a variable name as list. Here is what I have tried:

import math

mult_funcs = [math.sqrt, math.log]

test_func = list(map(lambda x: x, mult_funcs))
test_func(2)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-89-6561142477ba> in <module>
      1 test_func = list(map(lambda x: x, func_lst))
----> 2 test_func(2)

TypeError: 'list' object is not callable

Investigating SO led me to this solution: Apply multiple functions to the same argument in functional Python

Now, I have the code below which works. However, I am curious how to modify the lambda function so the value can change from 2 to any other number. In other words, I'd like to be able to do a regular function call (e.g., test_func(4)) instead of setting the lambda equal to a variable.

arg = 2
test_func = list(map(lambda x: x(arg), mult_funcs))
1
  • In the first variant you create a list (what the list(...) call does) and assign it to test_func. In the next line you try to call this list. In the second variant you can just replace the arg in the second line by 4 or anything else. First line can then be omitted. Commented Jan 12, 2020 at 1:57

2 Answers 2

2

If you want to call test_func it needs to be a function. So if you need to start with:

test_func = lambda x: ...

With that you can use map within that function and that map will need another lambda. Something like:

import math

mult_funcs = [math.sqrt, math.log]

test_func = lambda x: list(map(lambda f: f(x), mult_funcs))
test_func(2)
# [1.4142135623730951, 0.6931471805599453]

Which will capture the passed in number in a closure and apply the functions to it in the second lambda.

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm, why use lambda for test_func rather than def?
@HeapOverflow Because that’s the assignment. But I agree, a def makes sense.
1

Your first code snippet did not work because test_func at the end of lambda operation is a list and you can't do a function-call like thing on your list.

The most pythonic way would be to iterate over than using lambda:

import math

mult_funcs = [math.sqrt, math.log]

arg = 2
test_func = [x(arg) for x in mult_funcs]
# or: test_func = list(map(lambda x: x(arg), mult_funcs))

print(test_func)

In fact, the variable test_func is not a function here, but you get the idea.

1 Comment

That makes sense and I appreciate the alternative approach to solving this problem. In the spirit of the exercise question I was required to use map and lambda.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.