Modifying a Tuple - An Interesting Trick!
Suppose we have entered the values of a tuple's elements incorrectly or, within the program, we simply
need modifications.
The object is inherently
immutable, so we will use
a trick –
explicit conversion.
Analyze the code below:
Step 1. Lists are mutable objects, so we create one named
temp that contains all the elements of the tuple
pers1.
Step 2. Thus, we can rewrite the element at the second position in the
temp list accordingly.
Step 3. Now that everything is correct and as we wanted, we store the data in the
pers1 variable,
explicitly converting the
temp list to a
tuple using its constructor.
Done, we're finished!