How do we reassign tuples? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

How do we reassign tuples?

How do we reassign tuples? Please Help. Thanks. Whoever that answer my question and it was correct, I will follow him/her.

20th May 2021, 12:50 PM
SOON YEE FENG Moe
4 Answers
+ 2
tuples are imutables, meaning that you cannot change their content... however if the tuple store mutable types (almost non-primitives such as list, object...), you can update these elements (the references stored in the tuple remain unchanged). anyway, a variable wich hold a tuple could be reassigned without problem, with another tuple or anything else, that's why the workaround provided from the link given by Ananiya Jemberu (convert to list, update list, and convert back to new tuple) is perfectly valid... but the variable will hold a new (different) object reference at the end ^^
20th May 2021, 1:24 PM
visph
visph - avatar
+ 1
'''Method - 1 CHANGE IT TO A LIST,PERFORM THE REQUIRED OPERATIONS AND AGAIN CONVERT TO TUPLE''' t = ('275', '54000', '0.0', '5000.0', '0.0') lst = list(t) lst[0] = '300' t = tuple(lst) print(t) '''METHOD - 2 SLICING ''' b = (1, 2, 3, 4, 5) b = b[:2] + (8,9) + b[3:] print(b) '''METHOD - 3 (NOT A GOOD APPROACH THOUGH)''' new = 24 t = (1, 2, 3) t = (t[0],t[1],new) print(t)
20th May 2021, 1:18 PM
sarada lakshmi
sarada lakshmi - avatar
+ 1
IF YOU HAVE ACCESS TO google.com THEN TRY SEARCHING THIS "How do we reassign tuples?" YOU WILL GET RELEVANT ANSWERS! HAPPY SEARCHING! https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/gloss_python_change_tuple_item.asp https://stackoverflow.com/questions/11458239/how-to-change-values-in-a-tuple
21st May 2021, 4:47 AM
NEZ
NEZ - avatar