unwrapping argument tuples

Fredrik Lundh fredrik at effbot.org
Wed Dec 13 15:22:01 EST 2000


Prashanth Mundkur wrote:
> is there a way to unwrap a tuple argument to a function and
> match it to the parameters?
>
> that is, if
>
> >>>lst = [1, 2, 3]
>
> >>>def func (p1,p2,p3):
>        return p1+p2+p3
>
> then I want to apply func somewhat as
>
> >>>func(lst)
> 6
>
> such that the elements of lst are unwrapped and and matched
> to p1, p2, p3

    apply(func, lst)

or

    func(*lst) # 2.0 and later

also see:

    http://www.python.org/doc/FAQ.html#4.31
    "How do I call a function if I have the arguments in a tuple"

(in your case, lst is a list, not a tuple.  but in more recent version
of Python, apply accepts lists as well...)

</F>





More information about the Python-list mailing list