Insert an element into a Tuple in Python

avatar

Borislav Hadzhiev

Last updated: Jun 29, 2022

banner

Check out my new book

Insert an element into a Tuple in Python #

To insert an element into a tuple in Python:

  1. Use the list() class to convert the tuple to a list.
  2. Use the list.insert() method to insert the element into the list.
  3. Use the tuple() class to convert the list to a tuple.
main.py
my_tuple = ('a', 'b', 'c') # ✅ insert element into tuple using list.insert() my_list = list(my_tuple) my_list.insert(3, 'd') new_tuple = tuple(my_list) print(new_tuple) # 👉️ ('a', 'b', 'c', 'd') # ------------------------------ # ✅ insert element at the end of a tuple new_tuple_2 = my_tuple + ('d',) # 👈️ note comma print(new_tuple_2) # 👉️ ('a', 'b', 'c', 'd') # ------------------------------ # ✅ insert element at the beginning of a tuple new_tuple_3 = ('z',) + my_tuple print(new_tuple_3) # 👉️ ('z', 'a', 'b', 'c')

The code sample shows the 3 most common ways to insert an element into a tuple in Python.

Tuples are very similar to lists, but implement fewer built-in methods and are immutable (cannot be changed).

Since tuples cannot be changed, the only way to insert an element into a tuple is to create a new tuple that contains the element.

The first example converts the tuple to a list, uses the list.insert() method and converts the list back to a tuple.

main.py
my_tuple = ('a', 'b', 'c') my_list = list(my_tuple) my_list.insert(3, 'd') new_tuple = tuple(my_list) print(new_tuple) # 👉️ ('a', 'b', 'c', 'd')

The list class takes an iterable and returns a list object.

The list.insert method inserts an item at a given position.

The method takes the following 2 parameters:

NameDescription
indexThe index of the element before which to insert
itemThe item to be inserted at the given index

In the example, we added the string d at index 3 and used the tuple() class to convert the list back to a tuple.

Alternatively, you can use the addition (+) operator.

Use the addition (+) operator to insert an element into a tuple, e.g. new_tuple = my_tuple + ('new', ). Tuples are immutable, so in order to insert an element into a tuple, we have to create a new tuple that contains the element.

main.py
my_tuple = ('a', 'b', 'c') # ✅ insert element at the end of a tuple new_tuple_2 = my_tuple + ('d',) # 👈️ note comma print(new_tuple_2) # 👉️ ('a', 'b', 'c', 'd') # ------------------------------ # ✅ insert element at the beginning of a tuple new_tuple_3 = ('z',) + my_tuple print(new_tuple_3) # 👉️ ('z', 'a', 'b', 'c')

Notice that we wrapped the value in parenthesis and added a trailing comma, so that the values on the left-hand and right-hand sides of the addition (+) operator are tuples.

The trailing comma is significant and is the difference between creating a tuple and a string.
main.py
print(type(('a',))) # 👉️ <class 'tuple'> print(type(('a'))) # 👉️ <class 'str'>

Tuples are constructed in multiple ways:

  • Using a pair of parenthesis () creates an empty tuple
  • Using a trailing comma - a, or (a,)
  • Separating items with commas - a, b or (a, b)
  • Using the tuple() constructor

An alternative way to insert an element into a tuple is to use reassignment.

main.py
my_tuple = ('a', 'b', 'c') my_tuple += ('d',) # 👈️ note comma print(my_tuple) # 👉️ ('a', 'b', 'c', 'd')

This approach is useful when you don't need to keep access to the value of the tuple prior to inserting the element.

Instead of declaring a variable that stores the new tuple, we assign a new value to the original variable.

You can also insert an item to a tuple by unpacking the tuple into a new tuple.

main.py
my_tuple = ('a', 'b', 'c') new_tuple = (*my_tuple, 'd') print(new_tuple) # 👉️ ('a', 'b', 'c', 'd')

The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.

main.py
example = (*(1, 2), 3) # 👇️ (1, 2, 3) print(example)

The values from the tuple get unpacked into a new tuple where we can add extra elements.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.