The question is a bit misleading, because a tuple is immutable. What I want is:
Having a tuple a = (1, 2, 3, 4)
get a tuple b
that is exactly like a
except for the last argument which is, say, twice the last element of a
.
=> b == (1, 2, 3, 8)
b = a[:-1] + (a[-1]*2,)
What I'm doing here is concatenation of two tuples, the first containing everything but the last element, and a new tuple containing the mutation of the final element. The result is a new tuple containing what you want.
Note that for +
to return a tuple, both operands must be a tuple.
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