- 1
how to add or append an element to a tuple?
2 ответов
0
# try this way
>>> a = (1,2,3)
>>> a
(1,2,3)
>>> a = a.__add__((4,))
>>> a
(1,2,3,4)
# don't forget the comma after the 4
0
Can be done by unpacking a tuple and repacking it.
My_tup = (1,2,3)
x, y, z = My_tup #unpacks to x y and z
x += 3
My_tup = (x, y, z) #repacks tuple with new val
This works with small tuples, but is not efficient for larger ones.