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 can not be modified, whereas lists can, and they use parentheses instead of square brackets.

Though we can not change individual elements of a tuple, the del operator works with them. In this article, we are going to discuss the use of the del operator on a tuple.

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. And 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 in the tuple is used to delete the entire tuple. As tuples are immutable, they cannot delete a particular element in a tuple.

Example 1

In the following example, we explicitly remove an entire tuple using the del operator -

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

# Print the tuple
print(tup)

# Delete the tuple
del(tup)

print("After deleting the tuple:")

# Print the tuple
# This will raise an error
print(tup)

In the below output, you can observe that the del operator has deleted the entire tuple, and when we want to print the tuple after deleting the entire tuple, an error pops up.

('tutorials', 'point', 2022, True)
After deleting the tuple:
Traceback (most recent call last):
  File "main.py", line 5, in <module>
    print(tup)
NameError: name 'tup' is not defined

In lists, we use the del operator to delete a part of the list. As tuples are immutable, we cannot delete a slice of a tuple.

Example 2

In the example below, we will try to delete a part of the tuple using the del operator.

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

print("The below part of the tuple needs to be deleted")

# Print the part of the tuple to be deleted
print(tup[2:5])

# Delete the part of the tuple
del tup[2:5]

# Print the tuple
print("After deleting:")
print(tup)

When you run the program, it will show this output -

The below part of the tuple needs to be deleted
('the', 'best', 'platform')
Traceback (most recent call last):
  File "main.py", line 4, in <module>
del tup[2:5]
TypeError: 'tuple' object does not support item deletion
</module>

Example 3

In the below Python program, we will try to delete the nested tuple and also try to display it after deleting it -

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

# Print the original tuple
temp_tuple = nested_tuples[2]
print("Original nested structure:", nested_tuples)

del temp_tuple
print("Original tuple still intact:", nested_tuples)
# Delete the nested tuple
del nested_tuples
print("All tuples deleted: ", nested_tuples)

After running the program, you will get this result -

Original nested structure: (1, 2, (3, 4), (5, 6))
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    print("Original nested structure:", nested_tuples)
NameError: name 'nested_tuples' is not defined
Updated on: 2025-06-11T10:52:32+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements