Python - Add to Tuple


Python - Add to Tuple

To add to a tuple in Python, there is no direct way or a method. This is because of the tuple behavior that it cannot be modified. So, we have to use a workaround to add an item to a tuple.

The workaround to add an item to a tuple in Python is that, you have to convert the tuple to a list, then add the given item to the list, and convert the modified list back to tuple.

In this tutorial, we will go through some examples of how to add item(s) to a tuple in Python, using the above said workaround.

Examples to Add Item to Tuple

1. Add item to end of tuple

In the following program, we are given a tuple, and we have to add an item to the end of the tuple.

Follow these steps.

  1. Given a tuple, say my_tuple, and an item, item_1.
  2. Convert the tuple my_tuple to a list using list() built-in function, and store the list in my_list.
  3. Append the item item_1 to the list using list append() method.
  4. Convert the list list_1 to a tuple using tuple() builtin function, and assign the returned tuple to my_tuple.
  5. You may print the tuple to output using print().

Python Program

# Given tuple and item
my_tuple = ('apple', 'banana')
item_1 = 'cherry'

print(f"Given tuple    : {my_tuple}")

# Convert tuple to list
my_list = list(my_tuple)

# Add item to list
my_list.append(item_1)

# Convert list to tuple
my_tuple = tuple(my_list)

print(f"Modified tuple : {my_tuple}")

Output

Given tuple    : ('apple', 'banana')
Modified tuple : ('apple', 'banana', 'cherry')

2. Insert item at specific index in tuple

In the following program, we are given a tuple, and we have to add or insert an item at specific index in the tuple.

Follow these steps.

  1. Given a tuple, say my_tuple, index at which to add/insert the item in index variable, and an item in item_1.
  2. Convert the tuple my_tuple to a list using list() built-in function, and store the list in my_list.
  3. Insert the item item_1 in the list at specified index using list insert() method. Call insert() method on the list, and pass the index and item as arguments.
  4. Convert the list list_1 to a tuple using tuple() builtin function, and assign the returned tuple to my_tuple.
  5. You may print the tuple to output using print().

Python Program

# Given tuple and item
my_tuple = ('apple', 'banana')
index = 1
item_1 = 'cherry'

print(f"Given tuple    : {my_tuple}")

# Convert tuple to list
my_list = list(my_tuple)

# Add item to list
my_list.insert(index, item_1)

# Convert list to tuple
my_tuple = tuple(my_list)

print(f"Modified tuple : {my_tuple}")

Output

Given tuple    : ('apple', 'banana')
Modified tuple : ('apple', 'cherry', 'banana')

We have inserted the item 'cherry' at index=1 in the tuple.

Summary

In this tutorial of Python tuples, we learned how to add an item to a tuple, with the help of well detailed Python example programs. Since there is no direct way to add an item to a tuple, we have converted it to list, modified the list, and then converted the list back to tuple.