Add an element to a tuple in Python

avatar

Borislav Hadzhiev

Last updated: Jun 26, 2022

banner

Photo from Unsplash

Add an element to a tuple in Python #

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

main.py
# ✅ add element to tuple by creating a new tuple my_tuple = ('one', 'two') new_tuple = my_tuple + ('three', ) # 👈️ trailing comma is important # 👇️ ('one', 'two', 'three') print(new_tuple) # -------------------- # ✅ add element to tuple by reassigning my_tuple_2 = ('one', 'two') my_tuple_2 += ('three',) # 👈️ trailing comma is important # 👇️ ('one', 'two', 'three') print(my_tuple_2) # ------------------------- # ✅ add element to tuple by unpacking my_tuple_3 = ('one', 'two') my_str = 'three' new_tuple_3 = (*my_tuple_3, my_str) # 👇️ ('one', 'two', 'three') print(new_tuple_3)

The code sample shows the 3 most common ways to add an item to 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 add an element to a tuple is to create a new tuple that contains the element.

The first example uses the addition (+) operator to create a new tuple by combining 2 other tuples.

main.py
my_tuple = ('one', 'two') new_tuple = my_tuple + ('three', ) # 👈️ trailing comma is important # 👇️ ('one', 'two', 'three') print(new_tuple)

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

main.py
# 👇️ ('a', 'b', 'c') print(('a', 'b') + ('c', ))

Tuples are constructed in multiple ways:

  • Using a pair of parentheses () creates an empty tuple
  • Using a trailing comma - a, or (a,)
  • Separating items with commas - a, b or (a, b)
  • Using the tuple() constructor
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'>

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

main.py
my_tuple_2 = ('one', 'two') my_tuple_2 += ('three',) # 👈️ trailing comma is important # 👇️ ('one', 'two', 'three') print(my_tuple_2)

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

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

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

main.py
my_tuple_3 = ('one', 'two') my_str = 'three' new_tuple_3 = (*my_tuple_3, my_str) # 👇️ ('one', 'two', 'three') print(new_tuple_3)

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.