Map list item to function with arguments

Python

Python Problem Overview


Is there any way to map list items to a function along with arguments?

I have a list:

pages = [p1, p2, p3, p4, p5...]

And I have to call function myFunc corresponding to each list elements along with additional arguments such that the following can be computed

myFunc(p1, additionalArgument)
myFunc(p2, additionalArgument)

and so on...

Is there any elegant method for doing this?

Python Solutions


Solution 1 - Python

You can also use a lambda function:

map(lambda p: myFunc(p, additionalArgument), pages)

Solution 2 - Python

Use a list comprehension:

result = [myFunc(p, additionalArgument) for p in pages]

Solution 3 - Python

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 - Python

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 like map() 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSushant GuptaView Question on Stackoverflow
Solution 1 - PythonamarchioriView Answer on Stackoverflow
Solution 2 - PythonFred FooView Answer on Stackoverflow
Solution 3 - PythonSven MarnachView Answer on Stackoverflow
Solution 4 - PythonTomasz BartkowiakView Answer on Stackoverflow