Search code examples
pythontuplesiterable-unpacking

Tuple unpack in assignment


I would like to unpack a tuple in a python statement like so:

a = 5, *(6,7)

but this raises a SyntaxError. What is the cleanest way to achieve something like this?

The best I've come up with so far is:

a = tuple([5]+list((6,7)))

Solution

  • You can just concatenate the tuples directly:

    >>> a = (5,)+(6, 7)     
    >>> a
    (5, 6, 7)