- Published on
Appending to Tuples in Python: Breaking Down the Process
Because Python tuples are immutable objects there's no straightforward way to add an element to a tuple in Python or there is no append()
or extend()
methods like we use in list.
What is a tuple?
Tuples are one of the main data structures available within Python. In Python tuples are an unordered, immutable data type used to store collections. If you are already familiar with python, to create lists in python we use the square bracket syntax Tuple data types are initialized by encapsulating values using parentheses () and are separated by commas. Python tuples are container data types and they are heterogeneous meaning they can hold many different other data types at once.
Like Lists tuples are also ordered and indexed, meaning that we can access a tuples’ items using its index position starting from 0. All of this probably makes tuples sound very similar to Python lists, but they have one significant difference that tuples are immutable.
What does immutable mean
The python documentation defines immutable as objects whose value is unchangeable once they are created are called immutable. This is why tuples can not be changed or they are immutable because when we add additional elements to a tuple we are mutating or changing the data. In this article
Appending to a tuple means creating a new object that adds value to an existing tuple.
In this article you will learn how to add new elements to python tuples using methods such as list conversion, tuple concatenation, and tuple unpacking
Append to a Python Tuple Using list conversion
Since python tuples are immutable meaning they can’t be changed we need to convert the tuple to a list then 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.
Step 1: Convert tuple to list
To convert a tuple to a list in Python use the list()
method.
tup = (11, 21, 19, 18, 29)
lst = list(tup)
print(lst) # 👉️ [11, 21, 19, 18, 29]
print(type(lst)) # 👉️ <class 'list'>
The type()
is a built-in function that allows you to check the data type of the parameter passed to it.
Step 2: Append an element to the list
Use can the append()
method to append an element to a list in Python
tup = (11, 21, 19, 18, 29)
lst = list(tup)
lst.append(46)print(lst)
print(lst) # 👉️ [11, 21, 19, 18, 29, 46]
Step 3: Convert a list to tuple.
To convert a list to a tuple, use the tuple()
function.
tup = (11, 21, 19, 18, 29)
lst = list(tup)
lst.append(46)
tup = tuple(lst)
print(tup) # 👉️ (11, 21, 19, 18, 29, 46)
Nice we successfully append an element to a tuple using the list conversions and append() method. In the next section, you’ll learn how to use Python to append to a tuple using tuple unpacking .
Append to a Python Tuple Using Tuple Concatenation
Python allows us to combine two tuples by using the +
operator so in order to add item to a tuple we need to create new tuple with the items we want to add then concatenate the new and the old tuple In fact, when we do this, we actually create a new tuple, even if we apply it to the same variable as before.
Let’s see this with an example
tuple_one = (11, 21, 19)
tuple_concat = tuple_one + (46, )
print(tuple_concat) # 👉️ (11, 21, 19, 46)
You can see that we append a tuple to a tuple using the +
operator. This is because we are appending a tuple to a tuple, not an element. If you are appending an element to a tuple, it will throw a TypeError.
The other way to append item to a python tuple is using tuple unpacking.
Append to a Python Tuple Using Tuple Unpacking
Another method to append a value to a Python tuple is Tuple Unpacking. In this section 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.The unpacking operator, *
, is used to access all the elements in a container object, such as a tuple. So in order to use the unpacking operator to append to a tuple we need to unpack all the values of the first tuple and then add the new value or values.
tuple_one = (11, 21, 19)
tuple_two = (*tuple_one, 46)
print(tuple_two) # 👉️ (11, 21, 19, 46)
We unpack tuple_one and add the element 46 to that tuple, and we get the appended value tuple in the result.
In this tutorial, you learned how to use Python to append to a tuple. You learned that Python tuples are immutable meaning we can’t mutate them and You then learned how to append to a Python tuple using list conversion, tuple concatenation and tuple unpacking method.