Map list item to function with arguments

Question:

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?

Asked By: Sushant Gupta

||

Answers:

Use a list comprehension:

result = [myFunc(p, additionalArgument) for p in pages]
Answered By: Fred Foo

You could use a list comprehension

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

or functools.partial()

map(functools.partial(myFunc, some_arg=additionalArgument), pages)
Answered By: Sven Marnach

You can also use a lambda function:

map(lambda p: myFunc(p, additionalArgument), pages)
Answered By: amarchiori

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
Answered By: Tomasz Bartkowiak
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.