Skip to content

Update the Tuple

In Python, Tuple is an immutable sequence of elements. Once you create a tuple, you can not change its elements. However, you can update the tuple by converting it into the other data types. In this tutorial, we will learn how to update the tuple in Python.

Add Elements to the Tuple (Wrong Way)

You can not add elements to the tuple. If you try to add elements to the tuple, you will get an error.

tuple_add_elements.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data[10] = 'k'
tuple_add_elements.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data[10] = 'k'

Output:

command
C:\Users\username>python tuple_add_elements.py
Traceback (most recent call last):
  File "tuple_add_elements.py", line 2, in <module>
    data[10] = 'k'
TypeError: 'tuple' object does not support item assignment
command
C:\Users\username>python tuple_add_elements.py
Traceback (most recent call last):
  File "tuple_add_elements.py", line 2, in <module>
    data[10] = 'k'
TypeError: 'tuple' object does not support item assignment

In this example, we try to add an element to the tuple using the index number. The output shows that you can not add elements to the tuple. You will get an error if you try to add elements to the tuple.

How to Add Elements to the Tuple (Right Way)

You can add elements to the tuple by converting it into the other data types.

Add Elements to the Tuple Using the List

You can add elements to the tuple by converting it into the list. You can then add elements to the list. Finally, you can convert the list into the tuple.

tuple_add_elements_list.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data = list(data)
data.append('k')
data = tuple(data)
print(data)
tuple_add_elements_list.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data = list(data)
data.append('k')
data = tuple(data)
print(data)

Output:

command
C:\Users\username>python tuple_add_elements_list.py
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k')
command
C:\Users\username>python tuple_add_elements_list.py
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k')

In this example, we convert the tuple into the list using the list()list() function. We then add an element to the list using the append()append() method. Finally, we convert the list into the tuple using the tuple()tuple() function. The output shows that the element is added to the tuple.

Add Elements to the Tuple Using the String

You can add elements to the tuple by converting it into the string. You can then add elements to the string. Finally, you can convert the string into the tuple.

tuple_add_elements_string.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data = ''.join(data)
data += 'k'
data = tuple(data)
print(data)
tuple_add_elements_string.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data = ''.join(data)
data += 'k'
data = tuple(data)
print(data)

Output:

command
C:\Users\username>python tuple_add_elements_string.py
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k')
command
C:\Users\username>python tuple_add_elements_string.py
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k')

In this example, we convert the tuple into the string using the join()join() function. We then add an element to the string using the +=+= operator. Finally, we convert the string into the tuple using the tuple()tuple() function. The output shows that the element is added to the tuple.

Add Elements to the Tuple Using the Dictionary

You can add elements to the tuple by converting it into the dictionary. You can then add elements to the dictionary. Finally, you can convert the dictionary into the tuple.

tuple_add_elements_dict.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data = dict.fromkeys(data)
data['k'] = None
data = tuple(data)
print(data)
tuple_add_elements_dict.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data = dict.fromkeys(data)
data['k'] = None
data = tuple(data)
print(data)

Output:

command
C:\Users\username>python tuple_add_elements_dict.py
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k')
command
C:\Users\username>python tuple_add_elements_dict.py
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k')

In this example, we convert the tuple into the dictionary using the fromkeys()fromkeys() function. We then add an element to the dictionary using the [][] operator. Finally, we convert the dictionary into the tuple using the tuple()tuple() function. The output shows that the element is added to the tuple.

Add Elements to the Tuple Using the Set

You can add elements to the tuple by converting it into the set. You can then add elements to the set. Finally, you can convert the set into the tuple.

tuple_add_elements_set.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data = set(data)
data.add('k')
data = tuple(data)
print(data)
tuple_add_elements_set.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data = set(data)
data.add('k')
data = tuple(data)
print(data)

Output:

command
C:\Users\username>python tuple_add_elements_set.py
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k')
command
C:\Users\username>python tuple_add_elements_set.py
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k')

In this example, we convert the tuple into the set using the set()set() function. We then add an element to the set using the add()add() method. Finally, we convert the set into the tuple using the tuple()tuple() function. The output shows that the element is added to the tuple.

Add Elements to the Tuple Using the Bytes

You can add elements to the tuple by converting it into the bytes. You can then add elements to the bytes. Finally, you can convert the bytes into the tuple.

tuple_add_elements_bytes.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data = bytes(data, encoding='utf-8')
data += b'k'
data = tuple(data)
print(data)
tuple_add_elements_bytes.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data = bytes(data, encoding='utf-8')
data += b'k'
data = tuple(data)
print(data)

Output:

command
C:\Users\username>python tuple_add_elements_bytes.py
(b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k')
command
C:\Users\username>python tuple_add_elements_bytes.py
(b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k')

In this example, we convert the tuple into the bytes using the bytes()bytes() function. We then add an element to the bytes using the +=+= operator. Finally, we convert the bytes into the tuple using the tuple()tuple() function. The output shows that the element is added to the tuple.

Add Elements to the Tuple Using the Bytearray

You can add elements to the tuple by converting it into the bytearray. You can then add elements to the bytearray. Finally, you can convert the bytearray into the tuple.

tuple_add_elements_bytearray.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data = bytearray(data, encoding='utf-8')
data += b'k'
data = tuple(data)
print(data)
tuple_add_elements_bytearray.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data = bytearray(data, encoding='utf-8')
data += b'k'
data = tuple(data)
print(data)

Output:

command
C:\Users\username>python tuple_add_elements_bytearray.py
(97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107)
command
C:\Users\username>python tuple_add_elements_bytearray.py
(97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107)

In this example, we convert the tuple into the bytearray using the bytearray()bytearray() function. We then add an element to the bytearray using the +=+= operator. Finally, we convert the bytearray into the tuple using the tuple()tuple() function. The output shows that the element is added to the tuple.

Conclusion

In this tutorial, we learned how to update the tuple in Python. We learned how to add elements to the tuple by converting it into the other data types. We also learned how to add elements to the tuple using the list, string, dictionary, set, bytes, and bytearray. Now you can add elements to the tuple in Python. For more information, visit the official Python documentation. For more tutorials, visit our Python Central Hub.

Was this page helpful?

Let us know how we did