Python: Update Tuple

As we know tuples are immutable. Thus when the tuple is created we cannot change its value or update its value However we can workaround  by firstly , converting  tuple to list by built-in function list(). As we know, we can always update an item to list object assigning new value to element at certain index. Then use another built-in function tuple() to convert this list object back to tuple.

Example:

tup = (10,20,30,40,50)
list_1 = list(tup)
list_1[0] = 5
tup = tuple(list_1)
print(tup)

Output: (5, 20, 30, 40, 50)

Tags