Mango Mango - 4 months ago 7x
Python Question

How can I supply (multiple) arguments in the form of a List or Tuple?

Say I have this Python 3(.5) script:

# "g_args" = the arguments I would like to supply
g_args = ("Hello", "World")

# "greet" = the function I would like to supply the arguments to
def greet(phrase, person):
print("{0} {1}!".format(phrase, person))

How do I call
and supply the values in the
Tuple as the function arguments?

Maybe something like this:


NOTE: I would just like to point out that, I am indeed aware that you cannot do this:


Simply because this is equivalent to supplying one argument (the
Tuple), and not multiple arguments, I.E: the values in the

Thank you for your time and help in advance.


You want the * operator:

>>> greet(*g_args)
Hello World!

I don't know what it's called, but coming from Ruby, I call it the "splat operator" -- it allows you to pass in the elements of a list or tupe as the arguments to a method.

This question has more detailed information about what the * operator does. In short, it unpacks a list -- that is, any iterable, if memory serves -- and passes those elements into the method as its arguments. As far as the recieving method is concerned, it's exactly like you passed in the elements of the list as its arguments -- they're used in exactly the same way.