In this article we will discuss how to Append, Insert, Replace and Delete elements from a tuple in python.

In Python, tuples are immutable i.e. once created we can not change its contents.  But sometimes we want to modify the existing tuple, in that case we need to create a new tuple with updated elements only from the existing tuple.
Let’s see how to insert, modify and delete the elements from tuple.

Append an element in Tuple at end

Suppose we have a tuple i.e.

# Create a tuple
tupleObj = (12 , 34, 45, 22, 33 )

Now to append an element in this tuple, we need to create a copy of existing tuple and then add new element to it using + operator i.e.
# Append 19 at the end of tuple
tupleObj = tupleObj + (19 ,)

We will assign the new tuple back to original reference, hence it will give an effect that new element is added to existing tuple.
Contents of tuple will be now,
(12, 34, 45, 22, 33, 19)

A new Element is appended at the end of tuple.

Insert an element at specific position in tuple

To insert an element at a specific index in the existing tuple we need to create a new tuple by slicing the existing tuple and copying contents from it.

Suppose we have a tuple i.e.

# Create a tuple
tupleObj = (12 , 34, 45, 22, 33 )

As indexing starts from 0 in tuple, so to insert an element at index n in this tuple, we will create two sliced copies of existing tuple from (0 to n) and (n to end) i.e.
# Sliced copy containing elements from 0 to n-1
tupleObj[ : n]
# Sliced copy containing elements from n to end
tupleObj[n : ]

Now join these two sliced copies with new elements in between i.e.
n = 2
# Insert 19 in tuple at index 2
tupleObj = tupleObj[ : n ] + (19 ,) + tupleObj[n : ]

Now contents of tuple will be.
(12, 34, 19, 45, 22, 33, 19)

A new Element is inserted at index n.

Modify / Replace the element at specific index in tuple

To replace the element at index n in tuple we will use the same slicing logic as above, but we will slice the tuple from from (0 to n-1) and (n+1 to end) i.e.

# Sliced copy containing elements from 0 to n-1
tupleObj[ : n]
# Sliced copy containing elements from n to end
tupleObj[n + 1 : ]

None of the above sliced copies contains existing element at index n. Now join these two sliced copies with new elements in between i.e.
tupleObj =  (12, 34, 19, 45, 22, 33, 19)

n = 2
# Replace the element at index 2 to 'Test'
tupleObj = tupleObj[ : n] + ('test' ,) + tupleObj[n + 1 : ]

Now contents of tuple will be.
(12, 34, 'test', 45, 22, 33, 19)

Element in index n is replaced now.

Delete an element at specific index in tuple

To delete the element at index n in tuple we will use the same slicing logic as above, but we will slice the tuple from from (0 to n-1) and (n+1 to end) i.e.

# Sliced copy containing elements from 0 to n-1
tupleObj[ : n]
# Sliced copy containing elements from n to end
tupleObj[n + 1 : ]

None of the above sliced copies contains existing element at index n. Now join these two sliced copies i.e.
tupleObj =(12, 34, 'test', 45, 22, 33, 19)

n = 2
# Delete the element at index 2 
tupleObj = tupleObj[ : n ] + tupleObj[n+1 : ]

Now contents of tuple will be.
(12, 34, 45, 22, 33, 19)

Element in index n is deleted now.

Complete example is as follows,

def main():
    
    # Create a tuple from list by type casting
    tupleObj = (12 , 34, 45, 22, 33 )
    
    print("****** Append an element in Tuple at end ******")
    
    print("Original Tuple : ", tupleObj)
    
    # Append 19 at the end of tuple
    tupleObj = tupleObj + (19 ,)
    
    print("Modified Tuple : ", tupleObj)
    
    print("******* Insert an element at specific index in tuple *******")
    
    print("Original Tuple : ", tupleObj)
    
    n = 2
    # Insert 19 in tuple at index 2
    tupleObj = tupleObj[ : n] + (19 ,) + tupleObj[n : ]
    
    print("Modified Tuple : ", tupleObj)
    

    print("******* Modify / Replace the element at specific index in tuple *******")
    
    
    print("Original Tuple : ", tupleObj)
    
    
    n = 2
    # Replace the element at index 2 to 'Test'
    tupleObj = tupleObj[ : n] + ('test' ,) + tupleObj[n + 1 : ]
    
    print("Modified Tuple : ", tupleObj)
    
    print("******* Delete the element at specific index in tuple *******")
    
    print("Original Tuple : ", tupleObj)
    
    n = 2
    # Delete the element at index 2 
    tupleObj = tupleObj[ : n ] + tupleObj[n+1 : ]
    
    print("Modified Tuple : ", tupleObj)
           
if __name__ == '__main__':
    main()


Output:
****** Append an element in Tuple at end ******
Original Tuple :  (12, 34, 45, 22, 33)
Modified Tuple :  (12, 34, 45, 22, 33, 19)
******* Insert an element at specific index in tuple *******
Original Tuple :  (12, 34, 45, 22, 33, 19)
Modified Tuple :  (12, 34, 19, 45, 22, 33, 19)
******* Modify / Replace the element at specific index in tuple *******
Original Tuple :  (12, 34, 19, 45, 22, 33, 19)
Modified Tuple :  (12, 34, 'test', 45, 22, 33, 19)
******* Delete the element at specific index in tuple *******
Original Tuple :  (12, 34, 'test', 45, 22, 33, 19)
Modified Tuple :  (12, 34, 45, 22, 33, 19)

 

Join a list of 2000+ Programmers for latest Tips & Tutorials