So apparently I can't do this in Python (2.7):
x = (1, 2,)
(a, b, c) = (*x, 3)
It made sense in my head, but well... I could create a function:
make_tuple = lambda *elements: tuple(elements)
then I can do
(c, a, b) = make_tuple(3, *x)
but not, for example
(a, b, c) = make_tuple(*x, 3)
(a, b, c, d) = make_tuple(*x, *x)
y = [3, 4]
(a, b, c, d) = (*x, *y,)
So what I am asking is
My current guess for #2:
(a, b, c) = x + (3,)
(a, b, c, d) = x + x
(a, b, c, d) = x + tuple(y)
In response to question 1, read the PEP 448 and bug 2292. Also interesting is the discussion in the mailing list. In resume what you want should be allowed in Python 3.4. For question 2 see the other solutions.
Don't forget itertools. It's usually more readable as things get more complex
>>> from itertools import chain
>>> a,b,c = chain(x, (3,))
>>> a,b,c,d = chain(x, x)
>>> a,b,c,d = chain(x, y)
What you can do in Python 2.7 is:
(a, b), c = x, 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With