Borislav Hadzhiev
Last updated: Jun 29, 2022
Check out my new book
To insert an element into a tuple in Python:
list()
class to convert the tuple to a list.list.insert()
method to insert the element into the list.tuple()
class to convert the list to a tuple.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.
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.
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:
Name | Description |
---|---|
index | The index of the element before which to insert |
item | The 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.
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.
print(type(('a',))) # 👉️ <class 'tuple'> print(type(('a'))) # 👉️ <class 'str'>
Tuples are constructed in multiple ways:
()
creates an empty tuplea,
or (a,)
a, b
or (a, b)
tuple()
constructorAn alternative way to insert an element into a tuple is to use reassignment.
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.
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.
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.