- 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.
4 ответов
+ 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 ^^
+ 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)
+ 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