I donβt think there is a pythonic β’ way to solve this issue. But in my code this is a fairly common situation, so I wrote my own function for myself:
def applyfs(funcs, args): """ Applies several functions to single set of arguments. This function takes a list of functions, applies each to given arguments, and returns the list of obtained results. For example: >>> from operator import add, sub, mul >>> list(applyfs([add, sub, mul], (10, 2))) [12, 8, 20] :param funcs: List of functions. :param args: List or tuple of arguments to apply to each function. :return: List of results, returned by each of `funcs`. """ return map(lambda f: f(*args), funcs)
In your case, I will use it as follows:
applyfs([mean, std, var, fxn4 ...], mylist)
Note that you really do not need to use function names (as you would need to do, for example, PHP4), in a Python function itself is a callable object and can be stored in a list.
EDIT:
Or perhaps it would be more pythonic to use list comprehension instead of map :
results = [f(mylist) for f in [mean, std, var, fxn4 ...]]
source share