How to Add Element to a Tuple in Python

Tuples are an immutable data type in Python, which means that once they are created, they cannot be modified. However, sometimes it’s necessary to add elements to a tuple. To add elements to a tuple, you first need to convert the tuple into a list, then use the append() function, and convert it back to a tuple.

The following example shows how to add an element to a tuple in Python.

Using append() Function

We can use the append() function to add an element to a tuple by converting it to a list and then back to a tuple.

Suppose we have the following tuple:

# Declare tuple
my_tuple = (1, 2, 3)

# Convert tuple to list
my_list = list(my_tuple)

# Add element to list
my_list.append(4)

# Convert list back to tuple
my_tuple = tuple(my_list)

# Show updated tuple
print("Updated tuple:", my_tuple) 

Output: 👇️

Updated tuple: (1, 2, 3, 4)

In this example, we convert the tuple my_tuple to a list, add the element 4 to the list using the append() function, and then convert the list back to a tuple.