In this tutorial, you’ll learn how to use Python to append to a tuple. Because Python tuples are immutable objects, meaning they can’t be changed once they are created, appending to a tuple is a bit trickier than appending to, say, a Python list.
By the end of this tutorial, you’ll have learned what Python tuple immutability means, how to append to a tuple using list conversion, tuple concactenation, and tuple unpacking. You’ll also learn when you can modify contents in a tuple, using some fringe cases. Let’s get started!
The Quick Answer: Use Tuple Concatenation
Table of Contents
What are Python Tuples?
Python tuples are one of the main container data structures available within Python. They are generally created using regular parentheses ()
. Because they are container data types, they can hold different items and allow items of different data types, meaning that they are heterogeneous.
Lists are also ordered and indexed, meaning that we can access a tuples’ items using its index position. All of this probably makes tuples sound very similar to Python lists, but they have one significant difference: they are immutable.
What does immutable mean? The Python documentation defines immutable as having a fixed value and not able to be changed. This, of course, presents a problem if we want to use Python to append to a tuple. When we want to append a value to something, this in fact involves changing (or mutating) that item. Because of this, when we refer to appending to a tuple in Python throughout this tutorial, we actually mean creating a new object which appends a value to an existing tuple.
Let’s see what creating a tuple looks like in Python. We’ll instantiate a new tuple with a number of different values and then print out the type of the object using the type() function.
# Creating a Tuple
a_tuple = (1, 2, 3)
print(type(a_tuple))
# Returns: <class 'tuple'>
We can see here that by printing out the type of our object that this returned <class 'tuple'>
. Now, let’s see what happens when we try to modify the third item in our tuple.
# Trying to modify an element of a tuple
a_tuple = (1, 2, 3)
a_tuple[2] = 4
# Returns: TypeError: 'tuple' object does not support item assignment
We can see that this raises a TypeError
error. We know that Python lists have an .append()
method. Let’s see what happens when we try to apply this to our tuple.
# Trying to append to a tuple
a_tuple = (1, 2, 3)
a_tuple.append(4)
# Returns: AttributeError: 'tuple' object has no attribute 'append'
This returns an AttributeError
error. So how do we actually append to a Python tuple? In the Next section, you’ll learn how to use Python to append to a tuple by using tuple concatenation.
Want to learn more about Python for-loops? Check out my in-depth tutorial that takes your from beginner to advanced for-loops user! Want to watch a video instead? Check out my YouTube tutorial here.
Append to a Python Tuple Using Tuple Concatenation
While Python tuples don’t offer a dedicated method to append values, we can emulate appending to a tuple by using tuple concatenation. In fact, when we do this, we actually create a new tuple, even if we apply it to the same variable as before.
The way that this works is by using the +
operator, which allows us to combine two tuples.
Let’s say that we have one tuple (1, 2, 3)
and we want to append the value 4
to it. We can use the +
operator to concatenate the value onto our tuple. Let’s see what this looks like:
# Appending to a tuple with concatenation
a_tuple = (1, 2, 3)
a_tuple = a_tuple + (4,)
print(a_tuple)
# Returns: (1, 2, 3, 4)
We can see here that our tuple had a value appended to it. The way that we accomplished this is by first converting our value to a tuple. This needed to be done, since we can only concatenate a tuple to a tuple. If we simple tried to append the value, this would raise a TypeError
.
I do want to stress, though, that is an entirely new object. We can verify this printing out the id of the object before and after we append to it, using the id() function. Let’s take a look here:
# Confirming that modifying the tuple creates a new object
a_tuple = (1, 2, 3)
print(id(a_tuple))
a_tuple = a_tuple + (4,)
print(id(a_tuple))
# Returns:
# 140607969770880
# 140607969669824
We can see that the new tuple, despite being assigned to the same variable is a new object.
In the next section, you’ll learn how to use Python to append to a tuple using list conversion.
Want to learn how to get a file’s extension in Python? This tutorial will teach you how to use the os and pathlib libraries to do just that!
Append to a Python Tuple by List Conversion
In this section, you’ll learn how to use Python to append to a tuple by first converting the tuple to a list. Because Python lists are mutable, meaning they can be changed, we can use the list .append() method to append a value to it. Once we have appended the value, we can turn it back to a tuple.
Let’s see what this process looks like. Again, well use our previous example: we’ll take our tuple containing (1,2,3) and append the value of 4 to it.
# Appending to a tuple with list conversion
a_tuple = (1, 2, 3)
a_list = list(a_tuple)
a_list.append(4)
a_tuple = tuple(a_list)
print(a_tuple)
# Returns: (1, 2, 3, 4)
Let’s break down what we did here:
- We created our tuple, a_tuple
- We converted our tuple to a list
- We apples the .append() list method and appended our value
- We converted our list back to a tuple
In the next section , you’ll learn how to use tuple unpacking to append a value to a tuple.
Want to learn more about calculating the square root in Python? Check out my tutorial here, which will teach you different ways of calculating the square root, both without Python functions and with the help of functions.
Append to a Python Tuple with Tuple Unpacking
In this section, you’ll learn another method to append a value to a Python tuple. Specifically, you’ll learn how to use tuple unpacking (using the *) operator, to append a value to a tuple. Similar to the methods described above, you’ll actually be creating a new tuple, but without needing to first create a list.
The unpacking operator, *, is used to access all the items in a container object, such as a tuple. In order to append a value, we first unpack all the values of the first tuple, and then include the new value or values.
Let’s see what this looks like:
# Appending to a tuple with tuple unpacking
a_tuple = (1, 2, 3)
a_tuple = (*a_tuple, 4)
print(a_tuple)
# Returns: (1, 2, 3, 4)
Let’s break down what we did here:
- We created our first, original tuple
- We then generated a new tuple with the same name. In this tuple we unpacked all the values in the original tuple and then included the new value.
In the next section, you’ll learn about a fringe case where you can append to a tuple’s value.
Want to learn more about Python f-strings? Check out my in-depth tutorial, which includes a step-by-step video to master Python f-strings!
Modify a Tuple’s Contents by Appending a Value
There is one fringe exception where you can append to a tuple without raising an error. Specifically, if one of your tuples’ items is a mutable object, such as a list, you can modify the item.
Really, we’re not appending to the tuple itself, but to a list contained in the tuple.
Let’s see how we can do this:
# Appending to an item in a tuple
a_tuple = (1, 2, [1, 2, 3])
a_tuple[2].append(4)
print(a_tuple)
# Returns: (1, 2, [1, 2, 3, 4])
Let’s break down what we did here:
- We created our new tuple, where one item is a list
- We used the .append() method on our tuples’ list
The reason that this works is because we’re not modifying the tuple’s identity but just the object within it.
Want to learn how to calculate and use the natural logarithm in Python. Check out my tutorial here, which will teach you everything you need to know about how to calculate it in Python.
Conclusion
In this tutorial, you learned how to use Python to append to a tuple. You learned about why Python tuples are immutable and what benefits this can bring to your programs. You then learned how to append to a Python tuple using tuple concatenation, list conversion and tuple unpacking. Finally, you learned in which cases it is possible to modify a tuples contents by appending to a tuples’ value.
To learn more about Python tuples, check out the official documentation here.