How to append, insert, modify and delete elements in Tuple in Python ?
How to append to a tuple: This article is about how we can append, insert, modify and delete elements in Tuple.
As we know a tuple in Python stores ordered and immutable objects. It is one of data type which stores multiple items in a single variable where all the elements are placed inside parentheses () and separated by commas.
Syntax : sample_tuple = (element1, element2, element3, ...)
As tuple is immutable so once created values can not be changed. Still if we want to modify the existing tuple, then in that case we have to create a new tuple with updated elements only from the existing tuple. So let’s start exploring the topic to know how we can append, insert, modify and delete elements in Tuple.
- Python Program to get First Element of each Tuple in a List
- Python : How to Find an Element in Tuple by Value
- Python Program for set add() Method
Append an element in Tuple at end :
Append to tuple python: If we have a tuple and to append an element into it , then we will create copy of the existing tuple first then we will append the new element by using +
operator.
So , let’s see the implementation of it.
#Program : # A tuple created tuple_Obj = (1 , 3, 4, 2, 5 ) #printing old tuple print("Old tuple is :") print(tuple_Obj) # Appending 9 at the end of tuple tuple_Obj = tuple_Obj + (9 ,) #printing new tuple print("After appending new tuple is :") print(tuple_Obj)
Output : Old tuple is : (1 , 3, 4, 2, 5 ) After appending new tuple is : (1 , 3, 4, 2, 5, 9 )
Insert an element at specific position in tuple :
Python tuple append: If we want to insert a specific element at particular index, then we have to create a new tuple by slicing the existing tuple and copying elements of old tuple from it.
Suppose we have to insert at index n
then we have to create two sliced copies of existing tuple from (0 to n) and (n to end). Like
# containing elements from 0 to n-1 tuple_Obj [ : n] # containing elements from n to end tuple_Obj [n : ]
So, let’s see the implementation of it.
#Program : # A tuple created tuple_Obj = (1 , 3, 4, 2, 5 ) #printing old tuple print("Old tuple is :") print(tuple_Obj) n = 2 # Insert 9 in tuple at index 2 tuple_Obj = tuple_Obj[ : n ] + (9 ,) + tuple_Obj[n : ] #printing new tuple print("After appending new tuple is :") print(tuple_Obj)
Output : Old tuple is : (1 , 3, 4, 2, 5 ) After appending new tuple is : (1 , 3, 9, 4, 2, 5 )
Modify / Replace the element at specific index in tuple :
Append tuple python: If we want to replace the element at index n
in tuple then we have to use the same slicing logic as we used in the above example, But in this we have to slice the tuple from from (0 to n-1) and (n+1 to end) , as we will replace the element at index n
, so after replacing we are copying the elements again from n+1 index
of the old tuple.
So, let’s see the implementation of it.
#Program : # A tuple created tuple_Obj = (1 , 3, 4, 2, 5 ) #printing old tuple print("Old tuple is :") print(tuple_Obj) n = 2 # Insert 'program' in tuple at index 2 tuple_Obj = tuple_Obj[ : n] + ('program' ,) + tuple_Obj[n + 1 : ] #printing new tuple print("After appending new tuple is :") print(tuple_Obj)
Output : Old tuple is : (1 , 3, 4, 2, 5 ) After appending new tuple is : (1 , 3, 'program', 2, 5 )
Delete an element at specific index in tuple :
Tuple append python: If we want to delete an element at index n
in tuple then we have to use the same slicing logic as we used in the above example, means we will slice the tuple from from (0 to n-1) and (n+1 to end) , like
# containing elements from 0 to n-1 tuple_Obj [ : n] # containing elements from n to end tuple_Obj [n+1 : ]
So, let’s see the implementation of it.
#Program : # A tuple created tuple_Obj = (1 ,3, 'program', 4, 2, 5 ) #printing old tuple print("Old tuple is :") print(tuple_Obj) n = 2 # Deleting the element at index 2 tuple_Obj = tuple_Obj[ : n ] + tuple_Obj[n+1 : ] #printing new tuple print("After appending new tuple is :") print(tuple_Obj)
Output : Old tuple is : (1 , 3, 'program', 4, 2, 5 ) After appending new tuple is : (1 , 3, 4, 2, 5 )