Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a variable number of arguments to the map function

Tags:

python

Suppose I have a function whose the prototype is:

def my_func(fixed_param, *args)

I would like to run this function with multiple arguments (not necessary the same number of arguments for each run) such as:

res = map(partial(my_func, fixed_param=3), [[1, 2, 3], [1, 2, 3, 4]])

where [1, 2, 3] and [1, 2, 3, 4] respectively correspond to the first and second set of parameters for args.

But this line of code fails with the following error:

TypeError: my_func() got multiple values for keyword argument 'fixed_param'
like image 890
floflo29 Avatar asked Dec 11 '22 15:12

floflo29


1 Answers

I would not be so sure about map vs list comprehension performance, as oftentimes it's a question of the function you use. Anyway, your options are:

map(lambda x: my_func(3, *x), ...)

Or

from itertools import starmap

starmap(partial(my_func, 3), ...)

Like all function in itertools, starmap returns an iterator, hence you must pass it to the list constructor, if you want a list. That will be slower than a listcomp for sure.

EDIT Benchmarks:

In [1]: def my_func(x, *args):
   ...:     return (x, ) + args
   ...: 

In [2]: from functools import partial

In [3]: from itertools import starmap

In [4]: import random

In [5]: samples = [range(random.choice(range(10))) for _ in range(100)]

In [6]: %timeit map(lambda x: my_func(3, *x), samples)
10000 loops, best of 3: 39.2 µs per loop

In [7]: %timeit list(starmap(partial(my_func, 3), samples))
10000 loops, best of 3: 33.2 µs per loop

In [8]: %timeit [my_func(3, *s) for s in samples]
10000 loops, best of 3: 32.8 µs per loop

For the sake of comparison, lets change the function a bit

In [9]: def my_func(x, args):
       ...:     return (x, )  + tuple(args)
       ...: 

In [10]: %timeit [my_func(3, s) for s in samples]
10000 loops, best of 3: 37.6 µs per loop

In [11]: %timeit map(partial(my_func, 3), samples)
10000 loops, best of 3: 42.1 µs per loop

Once again, list comprehension is faster.

like image 82
Eli Korvigo Avatar answered Dec 28 '22 08:12

Eli Korvigo