I have the following Python code:
data = ['1', '4.6', 'txt']
funcs = [int, float, str]
How to call every function with data in corresponding index as an argument to the function?
Now I’m using the code:
result = []
for i, func in enumerate(funcs):
result.append(func(data[i]))
map(funcs, data) don’t work with lists of functions ( Is there builtin function to do that simpler?
You could use
zip* to combine many sequences together:then you could iterate on this new sequence and make each function apply on the corresponding data. Since you just want to collect them into a list, list comprehension is much better than a for-loop:
Note: * Use
itertools.izipif you are using Python 2.x and the lists are very long.)