Unpacking lists or tuples

To pass the elements of a list or tuple as arguments in a function, use this line of code (substituting your function name and list or tuple name):

myfunction( *my_list_or_tuple)

The following code defines and calls a function, passing a list as its argument, using the * operator:

>>> def func with three args(a, b, c): ... print "the arguments are:", a, b, c

>>> weapons = ['fear', 'surprise', 'ruthless efficiency'] >>> func with three args(*weapons)

the arguments are: fear surprise ruthless efficiency

Was this article helpful?

0 0

Post a comment