Google News
logo
Python Program to Change Tuple Values
Tuples are immutable, meaning that their values cannot be changed once they are created. However, we can create a new tuple with the updated values.

Here's an example :
my_tuple = (1, 2, 3)
print(my_tuple)  # Output: (1, 2, 3)

# We can create a new tuple with the updated values
new_tuple = my_tuple[0], 4, my_tuple[2]
print(new_tuple)  # Output: (1, 4, 3)
In this example, we created a new tuple called `new_tuple` with the updated value at index 1 (which is 2 in the original tuple) to 4. The other values remained the same.

Alternatively, we can convert the tuple into a list, modify the list, and then convert it back to a tuple.

Here's an example :
my_tuple = (1, 2, 3)
my_list = list(my_tuple)
my_list[1] = 4
new_tuple = tuple(my_list)
print(new_tuple)  # Output: (1, 4, 3)
In the above example, we converted `my_tuple` into a list `my_list`, modified the value at index 1 to 4, and then converted `my_list` back to a tuple `new_tuple`.