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)
Section titled “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.
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data[10] = 'k'Output:
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 assignmentIn 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.
Add Elements to the Tuple Using the List
Section titled “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.
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data = list(data)
data.append('k')
data = tuple(data)
print(data)Output:
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.
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data = ''.join(data)
data += 'k'
data = tuple(data)
print(data)Output:
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.
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data = dict.fromkeys(data)
data['k'] = None
data = tuple(data)
print(data)Output:
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.
Add Elements to the Tuple Using the Set
Section titled “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.
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
data = set(data)
data.add('k')
data = tuple(data)
print(data)Output:
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.
Add Elements to the Tuple Using the Bytes
Section titled “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.
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:
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.
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:
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.
Conclusion
Section titled “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.
flowchart TD A["t = (1, [2, 3])"] --> B["slot 0 -> the int 1"] A --> C["slot 1 -> a list"] D["t[0] = 9"] --> E["TypeError: does not support item assignment"] F["t[1].append(4)"] --> G["allowed -- the LIST is mutable"] G --> H["t is now (1, [2, 3, 4])"] C --> I["hash(t)"] I --> J["TypeError: unhashable type: 'list'"] J --> K["so this tuple cannot be a dict key"]
Check yourself
Section titled “Check yourself”-
`t = (1, [2, 3])`. What does `t[1].append(4)` do?
Verified. The tuple freezes its SLOTS, not the objects they point at. The list inside is fully mutable.
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.
-
Why does `hash((1, [2]))` raise?
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.
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.
-
How do you produce a tuple with one element changed?
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.
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.
-
You need an immutable record with named fields and a way to make modified copies. What fits best?
Both give readable field access with immutability. `NamedTuple._replace()` and `dataclasses.replace()` return a modified copy rather than mutating.
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.
Try it: Update Tuple Exercises
Section titled “Try it: Update Tuple Exercises”Exercise 1 – Convert and Modify
Section titled “Exercise 1 – Convert and Modify”Exercise 2 – Concatenate Tuples
Section titled “Exercise 2 – Concatenate Tuples”Exercise 3 – Delete and Recreate
Section titled “Exercise 3 – Delete and Recreate”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading