How to extend a tuple of tuples?
Frank Millman
frank at chagford.com
Fri Sep 9 02:47:42 EDT 2016
Hi all
This should be easy, but I cannot figure it out.
Assume you have a tuple of tuples -
a = ((1, 2), (3, 4))
You want to add a new tuple to it, so that it becomes -
((1, 2), (3, 4), (5, 6))
The obvious way does not work -
a += (5, 6)
((1, 2), (3, 4), 5, 6)
I have discovered that there is something new in python 3.5 that does work -
a = (*a, (5, 6))
((1, 2), (3, 4), (5, 6))
But how would you have done it before? I know you can convert the outer
tuple to a list, add the new tuple, and convert it back again. Is that the
only solution?
BTW, I do know that tuples are immutable, and that all of the above
operations create a new tuple.
Frank Millman
More information about the Python-list
mailing list