Python Tutorial for Beginners

Add and Change Tuple in Python

How to Change Tuple Values in Python?

As mentioned above, Python tuples are immutable. This implies that they can't be changed, added to, or removed after they've been created.

Tuple values cannot be changed. However, you can convert a tuple into a list to be able to change it.

Example:

x = ("cranberry", "blueberry", "apricot")

y = list(x)

y[1] = "avacado"

x = tuple(y)

print(x)

Output:

("cranberry", "avacado", "apricot")

How to Add Tuple Values in Python?

Tuples do not have an append() method since they are immutable, but there are other ways to add items to a tuple.

1. Convert tuple into a list: You can change it to a list, add your item(s), and then change it back to a tuple, just like the solution for modifying a tuple.

Example: 

thistuple = ("cranberry", "blueberry", "apricot")

y = list(thistuple)

y.append("orange")

thistuple = tuple(y)

Output:

('cranberry', 'blueberry', 'apricot', 'orange')

2. Add tuple to a tuple: You can add tuples to tuples, thus if you want to add one (or more) items, make a new tuple with the elements and add it to the existing tuple.

Example:

thistuple = ("cranberry", "blueberry", "apricot")

y = ("orange",)

thistuple += y

print(thistuple)

Output:

('cranberry', 'blueberry', 'apricot', 'orange')
Did you find this article helpful?