Flatten a list/tuple and Call a function with tuples

Wildemar Wildenburger wildemar at freakmail.de
Wed Jul 25 19:08:37 CEST 2007


kyosohma at gmail.com wrote:
> On Jul 25, 9:50 am, beginner <zyzhu2... at gmail.com> wrote:
>   
>> Another question is how do I pass a tuple or list of all the
>> aurgements of a function to the function. For example, I have all the
>> arguments of a function in a tuple a=(1,2,3). Then I want to pass each
>> item in the tuple to a function f so that I make a function call
>> f(1,2,3). In perl it is a given, but in python, I haven't figured out
>> a way to do it. (Maybe apply? but it is deprecated?)
>>     
> I'm not sure about the first question, but as for the second, you
> could do a few different things. You could just pass the tuple itself
> into the function and have the tuple unpacked inside the function.
>   
OR you could use the syntax invented for just that purpose ;).

 >>> t = 1, 2, 3
 >>> f(*t)

bam! :)

This works with dicts as well (for giving keyword arguments). There you 
prepend ** (two asterisk to your dict).
Simple :)
/W



More information about the Python-list mailing list