Map list item to function with arguments
Solution 1:
You can also use a lambda function:
map(lambda p: myFunc(p, additionalArgument), pages)
Solution 2:
Use a list comprehension:
result = [myFunc(p, additionalArgument) for p in pages]
Solution 3:
You could use a list comprehension
[myFunc(p, additionalArgument) for p in pages]
or functools.partial()
map(functools.partial(myFunc, some_arg=additionalArgument), pages)
Solution 4:
Note that if you're planning to use map
for distributed computations (i.e. using multiprocessing) it will not unpack the arguments as one could expect. Say you want to distribute your call to myFunc
(which accepts two arguments: page
and additionalArgument
) with:
pages = [p1, p2, p3, p4, p5...]
If you specify a list of args
(tuples), i.e.
args = [(page, additionalArgument) for page in pages]
map
will not unpack the args
tuple and will pass only one argument (tuple) to myFunc
:
pool.map(myFunc, args) # map does not unpack the tuple
You will need to use multiprocessing.starmap instead
starmap
is likemap()
except that the elements of the iterable are expected to be iterables that are unpacked as arguments.
i.e.
pool.starmap(myFunc, args) # tuples are unpacked and two arguments are passed to myFunc