how to add or append an element to a tuple? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

how to add or append an element to a tuple?

29th Sep 2016, 2:20 PM
Azarudeen Aman
Azarudeen Aman - avatar
2 Answers
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
29th Sep 2016, 4:54 PM
Giovanni Gatto
Giovanni Gatto - avatar
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.
29th Sep 2016, 8:28 PM
Luke Armstrong