How does the del operator work on a tuple in Python?

A tuple is an ordered and immutable collection of Python objects separated by commas. Like lists, tuples are sequences. Tuples differ from lists in that they cannot be modified, whereas lists can, and they use parentheses instead of square brackets.

Though we cannot change individual elements of a tuple, the del operator works with them. In this article, we are going to discuss how the del operator works on tuples in Python.

The del Operator with Tuples

The del operator in Python is used to delete variables from the current scope. When you use del on a tuple variable, you are not deleting items from the tuple, but you are deleting the reference to the tuple in memory.

Once the reference is deleted, the tuple object will no longer be accessible by that name. Python's garbage collector will free up the memory it occupied.

Syntax

The syntax of the del operator is as follows ?

del object_name

The del operator with tuples is used to delete the entire tuple. Since tuples are immutable, you cannot delete particular elements within a tuple.

Deleting an Entire Tuple

In the following example, we delete an entire tuple using the del operator ?

# Create a tuple
tup = ('tutorials', 'point', 2022, True)

# Print the tuple
print("Original tuple:", tup)

# Delete the tuple
del tup

print("After deleting the tuple:")

# This will raise an error
try:
    print(tup)
except NameError as e:
    print(f"Error: {e}")
Original tuple: ('tutorials', 'point', 2022, True)
After deleting the tuple:
Error: name 'tup' is not defined

Attempting to Delete Tuple Elements

Unlike lists, you cannot delete a slice or individual elements of a tuple because tuples are immutable ?

# Create a tuple
tup = ("Tutorialspoint", "is", "the", "best", "platform", "to", "learn", "new", "skills")

print("Tuple slice to delete:", tup[2:5])

# This will raise an error
try:
    del tup[2:5]
except TypeError as e:
    print(f"Error: {e}")
    
print("Tuple remains unchanged:", tup)
Tuple slice to delete: ('the', 'best', 'platform')
Error: 'tuple' object does not support item deletion
Tuple remains unchanged: ('Tutorialspoint', 'is', 'the', 'best', 'platform', 'to', 'learn', 'new', 'skills')

Deleting References to Nested Tuples

You can delete individual references to nested tuple elements, but this doesn't modify the original tuple ?

# Create a nested tuple
nested_tuples = (1, 2, (3, 4), (5, 6))

# Create a reference to a nested tuple
temp_tuple = nested_tuples[2]
print("Original nested structure:", nested_tuples)
print("Reference to nested tuple:", temp_tuple)

# Delete the reference
del temp_tuple

print("Original tuple still intact:", nested_tuples)

# Delete the entire nested tuple
del nested_tuples

try:
    print(nested_tuples)
except NameError as e:
    print(f"Error: {e}")
Original nested structure: (1, 2, (3, 4), (5, 6))
Reference to nested tuple: (3, 4)
Original tuple still intact: (1, 2, (3, 4), (5, 6))
Error: name 'nested_tuples' is not defined

Key Points

  • del removes the variable reference, not the tuple contents
  • You cannot delete individual elements or slices from a tuple
  • Once deleted, accessing the tuple raises a NameError
  • Deleting a reference to nested elements doesn't modify the original tuple

Conclusion

The del operator in Python works on tuples by deleting the entire tuple variable from memory. Unlike lists, you cannot delete individual elements or slices from tuples due to their immutable nature.

Updated on: 2026-03-24T19:08:40+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements