Modifying Python Tuples
Tuples are immutable, meaaning that once they are created you can't change them. You can, however, create a new tuple from an existing Tuple, and in doing so modify the result. This is generally achieved by converting the Tuple to a List, modifying the list, then converting back to a Tuple.
Adding a value to a Tuple
Python
Copy Code
letters = ("A", "B", "C") letterList = list(letters) letterList.append("D") letters = tuple(letterList) print(letters)
Output
('A', 'B', 'C', 'D')
Removing a value from a Tuple
Python
Copy Code
letters = ("A", "B", "C") letterList = list(letters) letterList.remove("B") letters = tuple(letterList) print(letters)
Output
('A', 'C')