How to add to a tuple in python?

A tuple is a built-in data type in python, that contains a sequence of elements separated by commas and enclosed within parentheses. The data items in the tuple are immutable which means we cannot add, remove or change the elements in the tuple by using the append(), extend(), etc.

If you really want to add a tuple then you have to create a new tuple with the addition of the element you want to add along with elements of the old tuple.

Adding to a tuple by using the + operator

We can add to a tuple, by using the concatenation operator(+) or by converting a tuple to a list, adding the elements, and then converting it back to a tuple. This method doesn't change the old objects but creates a new object.

Consider a tuple tuple_1 = (3, 5, "red", 8.5). The tuple_1 is added to (5, 1, 2) using + operator, which gives output as tuple_1 = (3, 5, 'red', 8.5, 5, 1, 2). We can see that id(object memory address) of the tuple before addition and after addition is different.

tuple_1 = (3, 5, "red", 8.5)
print("tuple_1: ", tuple_1)
print("ID : ", id(tuple_1))
tuple_1 = tuple_1 + (5, 1, 2)
print("tuple_1: ", tuple_1)
print("ID : ", id(tuple_1))

Output

tuple_1:  (3, 5, 'red', 8.5)
ID :  1378561222192
tuple_1:  (3, 5, 'red', 8.5, 5, 1, 2)
ID :  1378561131328

We can only concatenate tuples. If we try to concatenate other data types such as lists, sets, strings, etc. the program raises a TypeError exception.

tuple_1 = (3, 5, "red", 8.5)
print("tuple_1: ", tuple_1)
tuple_1 = tuple_1 + [5, 1]
print("tuple_1: ", tuple_1)

Output

tuple_1 = tuple_1 + [5, 1]
TypeError: can only concatenate tuple (not "list") to tuple

Adding to a tuple converting a tuple to a list

A tuple can be converted to a list using the list() constructor. The list is mutable, which allows the programmer to update or change the elements using append(), extend(), etc. After the addition of elements to the list, it is converted back to a tuple using the tup() constructor.

Consider a tuple tuple_1 = (3, 5, "red", 8.5). The tuple_1 is converted to list using list() and is stored in variable list_1. Multiple items are added to list_1 using extend() method. list_1 .extend([5, 1, 2]) change list_1 to [3, 5, 'red', 8.5, 5, 1, 2]. The list_1 is converted to tuple using tuple() and is stored in variable tuple_1.

The memory address of the tuple before and after converting it to a list is different.

tuple_1 = (3, 5, "red", 8.5)
print("tuple_1: ", tuple_1)
print("Id before conversion to list: ", id(tuple_1))
list_1 = list(tuple_1)
list_1.extend([5, 1, 2])
print("list_1: ", list_1)
tuple_1 = tuple(list_1)
print("tuple_1: ", tuple_1)
print("Id after conversion to list: ", id(tuple_1))

The above code returns the output as

tuple_1:  (3, 5, 'red', 8.5)
Id before conversion to list:  1287564486192
list_1:  [3, 5, 'red', 8.5, 5, 1, 2]
tuple_1:  (3, 5, 'red', 8.5, 5, 1, 2)
Id after conversion to list:  1287564474400

For example, consider a tuple tup= (3, 5, "red", 8.5). When we try to append elements using the append() method it raises an AttributeError exception.

tuple_1 = (3, 5, "red", 8.5)
print(tuple_1)
tuple_1.append(6)
print(tuple_1)

Output

tuple_1.append(6)
AttributeError: 'tuple' object has no attribute 'append'

Also, when we try to change the elements using index values it raises a TypeError exception.

tuple_1 = (3, 5, "red", 8.5)
print(tuple_1)
tuple_1[2] = "black"
print(tuple_1)

Output

tuple_1[2] = "black"
TypeError: 'tuple' object does not support item assignment
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions