I think the question can be read in two ways, apply a list of functions to an object and yield the result of each in a list (Single input, multiple output) OR apply a list of function to an object in sequence (Singe input, single output).
The latter is solved by the compose function, or by defining your own:
def compz(t, funcs):
return t if not funcs else compz(funcs[0](t), funcs[1:])
print(compz("str", [lambda x: x+"A", lambda x: x+"B", lambda x: x+"C"]))
print(compz("Hello, World", [lambda x: f"<h1>{x}</h1>", lambda x: f"<body>{x}</body>", lambda x: f"<html>{x}</html>",]))
print(compz(2, [lambda x: x+2, lambda x: x+5, lambda x: x+7]))
print(compz(2, [lambda x: x**2, lambda x: x**2, lambda x: x**2]))
print(compz(2, [lambda x: x**2, lambda x: x**2]))
print(compz(2, [lambda x: x**2]))
print(compz(2, []))
strABC
<html><body><h1>Hello, World</h1></body></html>
16
256
16
4
2
Most answers to the latter are equivalent, but really the list comprehension either because the functions are applied in "parallel" not in sequence or because you have to import a separate library. looking at a few answer, it can depend on what your result should look like, i.e. are you okay to have
func_list = [lambda x: x+"A", lambda x: x+"B", lambda x: x+"C"]
print([f("str") for f in func_list])
print([f for f in map(lambda f: f("str"), func_list)])
from functools import partial
def apply(f, a):
return f(a)
print([f for f in map(partial(apply, a="str"), func_list)])
def map_funcs(obj, func_list):
return [func(obj) for func in func_list]
print(map_funcs("str", func_list))
In all those cases the result is:
['strA', 'strB', 'strC']
[f(obj) for f in [foo,foo2]]? What do you want to do with the return values from the functions?mapandlambdaor list comprehensions. You can pick whichever one you want, but there isn't any "clean" alternative (and the functional style may be discouraged by some as "unpythonic").