Add, Update, Remove Tuple Items in Python

Modified: | Tags: Python

In Python, tuples are immutable, meaning you cannot directly add, update, or remove items. If you need mutable data, use a list instead.

However, if you need to modify a tuple, you can convert it into a list, make the necessary changes, and then convert it back into a tuple.

Note that even though terms like "add", "update", and "remove" are used for simplicity, in reality, a new object is created, and the original object remains unchanged.

Tuples are immutable

For example, consider the following tuple.

t = (0, 1, 2)

print(t)
# (0, 1, 2)

print(type(t))
# <class 'tuple'>

To access elements in a tuple, you can use indexing [] or slicing [:], just as you would with lists.

print(t[0])
# 0

print(t[:2])
# (0, 1)

Since tuples are immutable, you cannot assign a new value to an element.

# t[0] = 100
# TypeError: 'tuple' object does not support item assignment

Destructive methods that modify the original object, such as append() in lists, are not available in tuples.

# t.append(100)
# AttributeError: 'tuple' object has no attribute 'append'

Append an item to a tuple

Although tuples are immutable, you can concatenate them using the + operator. This process creates a new tuple without modifying the original one.

t = (0, 1, 2)

t_add = t + (3, 4, 5)

print(t_add)
# (0, 1, 2, 3, 4, 5)

print(t)
# (0, 1, 2)

You can concatenate a tuple only with another tuple, not with other types such as lists.

# print(t + [3, 4, 5])
# TypeError: can only concatenate tuple (not "list") to tuple

You can concatenate a list to a tuple by first converting the list to a tuple using tuple().

print(t + tuple([3, 4, 5]))
# (0, 1, 2, 3, 4, 5)

tuple() can only convert iterable objects, such as lists, to tuples. Non-iterable data types, such as integers (int) and floating-point numbers (float), cannot be converted.

# print(t + tuple(3))
# TypeError: 'int' object is not iterable

To append an item to a tuple, concatenate it as a single-element tuple.

print(t + (3,))
# (0, 1, 2, 3)

Remember that a single-element tuple requires a trailing comma.

Add/insert items into a tuple

To add new items at the beginning or end of a tuple, use the + operator as mentioned above. However, to insert a new item at any position, you must first convert the tuple to a list.

Convert a tuple to a list using list().

t = (0, 1, 2)

l = list(t)

print(l)
# [0, 1, 2]

print(type(l))
# <class 'list'>

You can insert an item using insert().

l.insert(2, 100)

print(l)
# [0, 1, 100, 2]

Convert the list back to a tuple with tuple().

t_insert = tuple(l)

print(t_insert)
# (0, 1, 100, 2)

print(type(t_insert))
# <class 'tuple'>

Update items in a tuple

You can update items in a tuple using the same approach. Convert the tuple to a list, update it, and then convert it back to a tuple.

t = (0, 1, 2)

l = list(t)
l[1] = 100
t_change = tuple(l)

print(t_change)
# (0, 100, 2)

Remove items from a tuple

Similarly, you can remove items from a tuple.

t = (0, 1, 2)

l = list(t)
l.remove(1)
t_remove = tuple(l)

print(t_remove)
# (0, 2)

In the above example, remove() is used, but you can also use pop() and del.

Related Categories

Related Articles