Delete Tuple Elements in Python

Rumman Ansari   Software Engineer   2023-06-01   342 Share
☰ Table of Contents

Table of Content:


The elements of a tuple cannot be changed, as was already said. Therefore, we are unable to eliminate or remove elements of a tuple.

However, the keyword del makes it feasible to delete a tuple completely.

Example


# Python program to show how to delete elements of a Python tuple  
  
# Creating a tuple  
tuple_ = ("Python", "Tuple", "Ordered", "Immutable", "Collection", "Objects")  
  
# Deleting a particular element of the tuple  
try:   
    del tuple_[3]  
    print(tuple_)  
except Exception as e:  
    print(e)  
  
# Deleting the variable from the global space of the program  
del tuple_  
  
# Trying accessing the tuple after deleting it  
try:  
    print(tuple_)  
except Exception as e:  
    print(e)  

Output


'tuple' object doesn't support item deletion
name 'tuple_' is not defined