Last updated: Apr 9, 2024
Reading timeยท5 min
Use the addition (+) operator to append an element to a tuple, e.g.
new_tuple = my_tuple + ('new', )
.
Tuples are immutable, so to append an element into a tuple, we have to create a new tuple that contains the element.
my_tuple = ('a', 'b', 'c') # โ Add an element at the end of a tuple new_tuple = my_tuple + ('d',) # ๐๏ธ note comma print(new_tuple) # ๐๏ธ ('a', 'b', 'c', 'd') # ------------------------------ # โ Add an element at the beginning of a tuple new_tuple = ('z',) + my_tuple print(new_tuple) # ๐๏ธ ('z', 'a', 'b', 'c')
The same approach can be used to append multiple elements to a tuple.
my_tuple = ('a', 'b', 'c') new_tuple = my_tuple + ('d', 'e') print(new_tuple) # ๐๏ธ ('a', 'b', 'c', 'd', 'e')
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.
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()
constructorIf you need to insert an element into a tuple:
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 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')
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 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 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 appending the element.
Instead of declaring a variable that stores the new tuple, we assign a new value to the original variable.
You can also unpack 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.
Alternatively, you can use tuple slicing.
def insert_into_tuple(tup, index, value): new_tuple = tup[:index] + (value,) + tup[index:] return new_tuple my_tuple = ('a', 'b', 'd') # ๐๏ธ ('a', 'b', 'c', 'd') print(insert_into_tuple(my_tuple, 2, 'c')) # ๐๏ธ ('z', 'a', 'b', 'd') print(insert_into_tuple(my_tuple, 0, 'z'))
The insert_into_tuple
function takes a tuple, an index and a value as
parameters and inserts the value into the tuple at the specified index.
The syntax for tuple slicing is my_tuple[start:stop:step]
.
start
index is inclusive and the stop
index is exclusive (up to, but not including).If the start
index is omitted, it is considered to be 0
, if the stop
index
is omitted, the slice goes to the end of the tuple.
Python indexes are zero-based, so the first item in a tuple has an index of 0
,
and the last item has an index of -1
or len(my_tuple) - 1
.
The slice tuple[:index]
starts at index 0
and goes up to, but not including
the specified index.
def insert_into_tuple(tup, index, value): new_tuple = tup[:index] + (value,) + tup[index:] return new_tuple
The slice tuple[index:]
starts at the specified index and goes to the end of
the tuple.
Notice that we added a trailing comma after the value we want to insert into the tuple.
It is necessary to wrap the value in a tuple to be able to use the addition (+) operator.
You can also convert the tuple to a list and use the append
method.
my_tuple = ('a', 'b', 'c') my_list = list(my_tuple) my_list.append('d') print(my_list) # ๐๏ธ ['a', 'b', 'c', 'd'] my_tuple = tuple(my_list) print(my_tuple) # ๐๏ธ ('a', 'b', 'c', 'd')
The code sample uses the list()
class to convert the tuple to a list and
appends elements to the list.
You can optionally convert the list back to a tuple by using the tuple()
class.
If you need to append multiple values to the tuple, use the list.extend()
method.
my_tuple = ('a', 'b', 'c') my_list = list(my_tuple) my_list.extend(['d', 'e']) print(my_list) # ๐๏ธ ['a', 'b', 'c', 'd', 'e'] my_tuple = tuple(my_list) print(my_tuple) # ๐๏ธ ('a', 'b', 'c', 'd', 'e')
You can learn more about the related topics by checking out the following tutorials: