We know that tuple is an immutable data type unlike a python dictionary or a list. That means, we cannot modify a tuple by any means. But, We may need to modify a tuple. In this case, we have no other option to create a new tuple with the desired elements. In this article, we will use different ways like slicing, packing, and unpacking to modify a tuple.
How to append an element to a tuple
To append an element at the end of a tuple, we can use tuple concatenation, just like we use string concatenation to append a string to another string. For this, first, we will create a tuple with a single element which has to be appended to the tuple. Then we will concatenate the new tuple and existing tuple using the + operator as follows.
myTuple = (1, 2, 3, 4)
print("Original Tuple is:", myTuple)
value = 5
print("Value to be added:", value)
newTuple = (5,)
myTuple = myTuple + newTuple
print("Updated tuple is:", myTuple)
Output:
Original Tuple is: (1, 2, 3, 4)
Value to be added: 5
Updated tuple is: (1, 2, 3, 4, 5)
We can also append an element to the start of the tuple using the above method as follows.
myTuple = (1, 2, 3, 4)
print("Original Tuple is:", myTuple)
value = 5
print("Value to be added:", value)
newTuple = (5,)
myTuple = newTuple + myTuple
print("Updated tuple is:", myTuple)
Output:
Original Tuple is: (1, 2, 3, 4)
Value to be added: 5
Updated tuple is: (5, 1, 2, 3, 4)
For appending element to the tuple, we can also use packing and unpacking using the * operator. First, we will unpack the existing tuple using the * operator. After that, we will create a new tuple including the new element as follows.
myTuple = (1, 2, 3, 4)
print("Original Tuple is:", myTuple)
value = 5
print("Value to be added:", value)
myTuple = (*myTuple, value)
print("Updated tuple is:", myTuple)
Output:
Original Tuple is: (1, 2, 3, 4)
Value to be added: 5
Updated tuple is: (1, 2, 3, 4, 5)
Just like appending an element to the end of a tuple, we can append an element to the start of a tuple using packing and unpacking as follows.
myTuple = (1, 2, 3, 4)
print("Original Tuple is:", myTuple)
value = 5
print("Value to be added:", value)
myTuple = (value, *myTuple)
print("Updated tuple is:", myTuple)
Output:
Original Tuple is: (1, 2, 3, 4)
Value to be added: 5
Updated tuple is: (5, 1, 2, 3, 4)
How to insert an element at a specific position in a tuple
To insert an element at a specific position in a tuple, we can use slicing. If we have to insert an element at index “i” of the tuple, we will slice the tuple and create two new tuples. The first tuple will contain elements from index 0 to i-1 of the original tuple. The second tuple will contain elements from index “i” to last. After that, we will create a tuple with a single element which has to be inserted into the tuple at index i. Then we will concatenate the three tuples to insert the new element at index “i” as follows.
myTuple = (1, 2, 3, 4, 5, 6, 7, 8)
print("Original Tuple is:", myTuple)
value = 0
print("Value to be inserted at index 3:", value)
left_tuple = myTuple[0:3]
right_tuple = myTuple[3:]
myTuple = left_tuple + (value,) + right_tuple
print("Updated tuple is:", myTuple)
Output:
Original Tuple is: (1, 2, 3, 4, 5, 6, 7, 8)
Value to be inserted at index 3: 0
Updated tuple is: (1, 2, 3, 0, 4, 5, 6, 7, 8)
We can also use packing and unpacking to merge the newly created tuples as follows.
myTuple = (1, 2, 3, 4, 5, 6, 7, 8)
print("Original Tuple is:", myTuple)
value = 0
print("Value to be inserted at index 3:", value)
left_tuple = myTuple[0:3]
right_tuple = myTuple[3:]
myTuple = (*left_tuple, value, *right_tuple)
print("Updated tuple is:", myTuple)
Output:
Original Tuple is: (1, 2, 3, 4, 5, 6, 7, 8)
Value to be inserted at index 3: 0
Updated tuple is: (1, 2, 3, 0, 4, 5, 6, 7, 8)
How to modify a tuple by deleting an element
To delete an element from the start of a tuple, we will create a new tuple using the rest of the elements as follows.
myTuple = (1, 2, 3, 4, 5, 6, 7, 8)
print("Original Tuple is:", myTuple)
myTuple = myTuple[1:]
print("Updated tuple is:", myTuple)
Output:
Original Tuple is: (1, 2, 3, 4, 5, 6, 7, 8)
Updated tuple is: (2, 3, 4, 5, 6, 7, 8)
If we need to delete the last element of a tuple, we will slice out the remaining elements as follows.
myTuple = (1, 2, 3, 4, 5, 6, 7, 8)
print("Original Tuple is:", myTuple)
tupleLength = len(myTuple)
myTuple = myTuple[0:tupleLength - 1]
print("Updated tuple is:", myTuple)
Output:
Original Tuple is: (1, 2, 3, 4, 5, 6, 7, 8)
Updated tuple is: (1, 2, 3, 4, 5, 6, 7)
To delete an element present at an index “i”, we will create two slices of the original tuple. The first slice will contain elements from index 0 to i-1 of the original tuple. The second tuple will contain elements from index “i+1” to last. After that, we will concatenate the newly created tuples, excluding the element at index “i” as follows.
myTuple = (1, 2, 3, 4, 5, 6, 7, 8)
print("Original Tuple is:", myTuple)
left_tuple = myTuple[0:3]
right_tuple = myTuple[4:]
myTuple = left_tuple + right_tuple
print("Updated tuple after deleting element at index 3 is:", myTuple)
Output:
Original Tuple is: (1, 2, 3, 4, 5, 6, 7, 8)
Updated tuple after deleting element at index 3 is: (1, 2, 3, 5, 6, 7, 8)
How to modify an element at a specific index in a tuple
To modify an element at index “i” of a tuple, we will create two slices of the original tuple. The first slice will contain elements from index 0 to i-1 of the original tuple. The second slice will contain elements from index “i+1” to last. After that, we will update the element at index “i” by inserting the new value at the position as follows.
myTuple = (1, 2, 3, 4, 5, 6, 7, 8)
print("Original Tuple is:", myTuple)
left_tuple = myTuple[0:3]
right_tuple = myTuple[4:]
myTuple = left_tuple + (100,) + right_tuple
print("Updated tuple after modifying element at index 3 is:", myTuple)
Output:
Original Tuple is: (1, 2, 3, 4, 5, 6, 7, 8)
Updated tuple after modifying element at index 3 is: (1, 2, 3, 100, 5, 6, 7, 8)
We can also use the * operator to merge the newly created tuples as follows.
myTuple = (1, 2, 3, 4, 5, 6, 7, 8)
print("Original Tuple is:", myTuple)
left_tuple = myTuple[0:3]
right_tuple = myTuple[4:]
myTuple = (*left_tuple, 100, *right_tuple)
print("Updated tuple after modifying element at index 3 is:", myTuple)
Output:
Original Tuple is: (1, 2, 3, 4, 5, 6, 7, 8)
Updated tuple after modifying element at index 3 is: (1, 2, 3, 100, 5, 6, 7, 8)
Conclusion
In this article, We have used different ways to modify a tuple in python. To read more about python programming, read this article on list comprehension. Stay tuned for more informative articles.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.