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)


    func(*lst) # 2.0 and later

also see:

    "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...)


More information about the Python-list mailing list