Packing values into a tuple using *, just like function argument packing
The closest way I know (that I'll admit, doesn't directly answer your question) is just to slice the tuple:
t = (2,3,4)a, args = t[0], t[1:]>>> type(args)<class 'tuple'>
Yes, it's verbose, but at least it avoids the intermediate list.
There are some additional considerations here to keep in mind. The type of the RHS is not restricted to list or tuple. You can unpack an arbitrary iterable:
a, *args = itertools.repeat('hi', 3)
There is no good reason that args
should be either type in particular. It makes sense that the result is a list since it should be able to accumulate an arbitrary number of elements, but there is no major relevant difference in the C code, at least in CPython.
Function arguments pretty much have to be a tuple because the function can return the packed object and a closure. You do not want to be able to modify args
in this case:
def func(arg, *args): assert args def blah(): return args[0] return args, blah
This is a bit contrived, but illustrates why args
can be considered public and should not be mutable in a function.
All that being said, here is a general purpose method for getting the unpacking you want, regardless of the RHS type:
t = (2, 3, 4)it = iter(t)a, args = next(it), tuple(it)