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.

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'

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

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)

Section titled “How to Add Elements to the Tuple (Right Way)”

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

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)

Output:

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() function. We then add an element to the list using the append() method. Finally, we convert the list into the tuple using the tuple() function. The output shows that the element is added to the tuple.

Add Elements to the Tuple Using the String

Section titled “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)

Output:

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() function. We then add an element to the string using the += operator. Finally, we convert the string into the tuple using the tuple() function. The output shows that the element is added to the tuple.

Add Elements to the Tuple Using the Dictionary

Section titled “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)

Output:

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() function. We then add an element to the dictionary using the [] operator. Finally, we convert the dictionary into the tuple using the tuple() function. The output shows that the element is added to the tuple.

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)

Output:

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() function. We then add an element to the set using the add() method. Finally, we convert the set into the tuple using the tuple() function. The output shows that the element is added to the tuple.

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)

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')

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

Add Elements to the Tuple Using the Bytearray

Section titled “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)

Output:

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() function. We then add an element to the bytearray using the += operator. Finally, we convert the bytearray into the tuple using the tuple() function. The output shows that the element is added to the tuple.

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.


diagram a tuple protects its own slots, not the objects in them mermaid
Immutability is one level deep. The tuple guarantees that its slots always point at the same objects; it says nothing about whether those objects can change. A tuple holding a list is therefore only partly frozen -- and that is also what makes it unhashable.
sketch What you can and cannot change about a tuple p5.js
The slots are frozen; the objects they point at may not be. Rebinding a slot raises, appending to a list inside one succeeds, and the tuple prints differently afterwards. The consequence is that a tuple is hashable only when everything inside it is -- which is why one containing a list cannot be a dictionary key.
pch.quizTag pch.quizDefaultTitle
  1. `t = (1, [2, 3])`. What does `t[1].append(4)` do?

    pch.quizShowAnswer

    B — It works; `t` becomes `(1, [2, 3, 4])` — Verified. The tuple freezes its SLOTS, not the objects they point at. The list inside is fully mutable.

  2. Why does `hash((1, [2]))` raise?

    pch.quizShowAnswer

    B — A tuple's hash depends on its contents, and a list has no hash — A tuple is hashable exactly when everything inside it is. That is what stops a tuple containing a list from being used as a dictionary key.

  3. How do you produce a tuple with one element changed?

    pch.quizShowAnswer

    C — Build a new one: `t[:1] + (99,) + t[2:]` — There is no in-place update. Slicing and concatenation build a new tuple, and any name still pointing at the old one is unaffected — which is the property you chose a tuple for.

  4. You need an immutable record with named fields and a way to make modified copies. What fits best?

    pch.quizShowAnswer

    B — A `NamedTuple` or a frozen dataclass — Both give readable field access with immutability. `NamedTuple._replace()` and `dataclasses.replace()` return a modified copy rather than mutating.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading