How to change a value of a tuple in Python
Python is the language for newly evolving technologies and has become one of the most popular programming languages in the world. It is used in everything right, from simple algorithm projects to high-level machine-level algorithms. Many web developers and programmers use Python. It has become the most recommended language nowadays. It is a general-purpose language that uses a wide range of applications.
Python language has many Data types, which are used to store data, whereas a tuple is also one of the data types. Tuples store multiple items in a single variable. Tuples are ordered because they maintain the data insertion order. These are unchangeable. Also, we cannot modify any element in a tuple. And tuples allow duplicate values where you can repeat the values of any component present in the tuple. So as we know, once we add elements to a tuple, they will remain unchanged, but there is a way to change a value of a tuple in Python. This article lets us see how we can change a value in a tuple.
To change a value in a tuple, we cannot change a tuple. Still, we can convert this data type into another data type and complete the conversion so we can convert the tuple into a list then change those values in that list cause lists are mutable or changeable. After changing the values, you can convert the list back to the tuple. To understand this, let us look at an example code
x = ("red", "blue", "green","yellow","black","purple", "orange")
#changing tuple into a list
y = list(x)
#changing the values from the list
y[1] = "brown"
y[4] = "white"
#converting list back to tuple
x = tuple(y)
#printing the changed string
print(x)
Output
As you can see in the above code, we first created a tuple with the name x containing colors like red, blue, green, yellow, black, purple, and orange. Then, we assigned a new value, y, to be the list form of x (tuple). And as we usually change values in a list, we changed this also using indexing, and we wanted to change the element in the first index with brown and the element in the fourth index with white. After changing the values, we again convert that list back to a tuple, and finally, we print the modified tuple.
After execution of the code, we can see that the element in the first index that was blue got changed into brown, and the element in the fourth index black got changed into white, and again the changed tuple got printed. So this is how we convert elements in a tuple
Let us look at another example code below:
tuple_new = (5, "hello", 2, 8, 4, 4, "bye", 2)
#changing tuple to list
list = list(tuple_new)
#updating the list
list[2] = "world"
#changing back from a list to a tuple
tuple_new = tuple(list)
#printing changed tuple
print(tuple_new)
Output
So in this example, we created a valid tuple where this tuple stored many types of data. There is an integer along with a string. This tuple also consists of a duplicate value in it. So now we converted this tuple of name tuple_new into a list. Then we updated that list by changing the element in the second index (2 into “world”), and then we again converted the list back into a tuple, and then we printed the converted tuple. After the execution in the whole tuple, the element in the second index changed into “world,” as we wanted.
Even in changing the values of the tuple, there is no need to maintain the same data type, like string to string or integer to an integer; we can also convert the string into an integer or an integer into a string. There will be no errors in those conversions.
Conclusion
In this way of converting a data type into another data type, we can change values in an unchangeable data type like a tuple. To change the elements in the tuple, we used a list as our primary data type to convert the elements. After changing the elements, we converted the tuple into a list and then converted the elements back to the tuple format.