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))
list(...)call does) and assign it totest_func. In the next line you try to call this list. In the second variant you can just replace theargin the second line by4or anything else. First line can then be omitted.