Reverse of map()?

Alex Martelli aleaxit at yahoo.com
Sun Feb 5 20:36:15 EST 2006


Phillip Sitbon <phillip.sitbon at gmail.com> wrote:

> Hello there,
> 
> I have a situation where a list of functions need to be called with a
> single set of parameters and the result constructed into a tuple. I
> know there's simple ways to do it via list comprehension:
> 
> Result = tuple(  [ fn(* Args, ** Kwds) for fn in fn_list ]  )
> 
> I'd hope there's a more efficient way to do this with a built-in
> function, so that I could call:
> 
> Result = rmap( fn_list, * Args, ** Kwds )
> 
> and have it constructed as a tuple from the get-go.
> 
> Is there a built-in function that would allow me to do this, or do I
> have to go with the list comprehension?

A genexp is probably going to be more efficient than the list
comprehension: just omit the brackets in your first snippet.

map(apply, fn_list, ...) may work, but I doubt it's going to be either
simple or speedy since the ... must be replaced with as many copies of
Args and Kwds as there are functions in fn_list, e.g.:

map(apply, fn_list, len(fn_list)*(Args,), len(fn_list)*(Kwds))


There's no built-in that calls many functions with identical args and
kwds, since it's a rare need.  Also, map returns a list, not a tuple, so
it's bordering on the absurd to think that your dreamed-for rmap would
be designed to return a tuple rather than a list -- you still have to
call tuple on the result, any way you build it.


Alex



More information about the Python-list mailing list