503

Is there a way to expand a Python tuple into a function - as actual parameters?

For example, here expand() does the magic:

some_tuple = (1, "foo", "bar")

def myfun(number, str1, str2):
    return (number * 2, str1 + str2, str2 + str1)

myfun(expand(some_tuple)) # (2, "foobar", "barfoo")

I know one could define myfun as myfun((a, b, c)), but of course there may be legacy code. Thanks

AkiRoss
  • 10,766
  • 6
  • 55
  • 84

6 Answers6

883

myfun(*some_tuple) does exactly what you request. The * operator simply unpacks the tuple (or any iterable) and passes them as the positional arguments to the function. Read more about unpacking arguments.

Nicolas Gervais
  • 28,901
  • 11
  • 96
  • 121
Alex Martelli
  • 811,175
  • 162
  • 1,198
  • 1,373
  • 11
    The * operator simply unpacks the tuple and passes them as the positional arguments to the function. See more here: https://docs.python.org/3/tutorial/controlflow.html#tut-unpacking-arguments – john_mc Jun 30 '17 at 21:09
  • 4
    Note that the same syntax can be used for lists as well as tuples. – brendon-ai Aug 17 '17 at 13:22
  • I've found that you can do the same with lists, (in fact, any iterable, including strings), not sure how their mutability affects things. That would be interesting to look into. – wcyn Nov 26 '17 at 11:47
61

Note that you can also expand part of argument list:

myfun(1, *("foo", "bar"))
Valentas
  • 1,744
  • 17
  • 23
  • 16
    It appears you can only do this if the expanded tuple is after the normally-provided arguments - the interpreter doesn't like it when I do this: `some_func(*tuple_of_stuff, another_argument)` – Tom Galvin Apr 19 '15 at 20:46
  • 6
    @Quackmatic Having the expanded tuple in any location seems to work fine in Python 3.5.1 – River Jun 13 '16 at 13:00
  • 1
    @Quackmatic seconding @River, this works fine in Python 3.5.4: `def func(a,b,c,d): print(a,b,c,d)` with `args = ('fee', 'fi', 'fo'); func(*args, 'fum')` – R. Navega Sep 06 '18 at 13:25
16

Take a look at the Python tutorial section 4.7.3 and 4.7.4. It talks about passing tuples as arguments.

I would also consider using named parameters (and passing a dictionary) instead of using a tuple and passing a sequence. I find the use of positional arguments to be a bad practice when the positions are not intuitive or there are multiple parameters.

Kellen Donohue
  • 769
  • 7
  • 16
Uri
  • 86,748
  • 48
  • 217
  • 319
10

This is the functional programming method. It lifts the tuple expansion feature out of syntax sugar:

apply_tuple = lambda f, t: f(*t)

Redefine apply_tuple via curry to save a lot of partial calls in the long run:

from toolz import curry
apply_tuple = curry(apply_tuple)

Example usage:

from operator import add, eq
from toolz import thread_last

thread_last(
    [(1,2), (3,4)],
    (map, apply_tuple(add)),
    list,
    (eq, [3, 7])
)
# Prints 'True'
Mateen Ulhaq
  • 21,459
  • 16
  • 82
  • 123
Dominykas Mostauskis
  • 7,169
  • 2
  • 46
  • 63
4

Similar to @Dominykas's answer, this is a decorator that converts multiargument-accepting functions into tuple-accepting functions:

apply_tuple = lambda f: lambda args: f(*args)

Example 1:

def add(a, b):
    return a + b

three = apply_tuple(add)((1, 2))

Example 2:

@apply_tuple
def add(a, b):
    return a + b

three = add((1, 2))
Mateen Ulhaq
  • 21,459
  • 16
  • 82
  • 123
0

features[2] is a tuple ('White', 'Unemployed', 'Income')

now to use features[2] as a parameter's list for columns

all_data[list(np.asarray(features[2]))]
thistleknot
  • 949
  • 13
  • 29