Python del Statement: Deleting Variables and Data Structures

# Delete variable binding from namespace
x = 100
print(x)  # 100

del x
# print(x)  # Raises NameError: name 'x' is not defined
del variable_name
The del statement is used in Python to unbind variables, delete items from collections like lists and dictionaries, or remove attributes from objects.

Unlike methods such as list.pop() or dict.pop(), which remove items and return their values, del is a statement that solely performs deletion. When applied to a variable, it removes the name binding from the local or global namespace. If no other references point to the underlying object, Python's garbage collector reclaims its memory. For variable assignments, see Variables, and for list manipulation, review List Methods and list.remove().

UsageDescriptionExample
VariablesRemoves name binding from namespacedel x
ListsRemoves item by index or slicedel my_list[0]
DictionariesRemoves key-value pair by keydel my_dict['key']
AttributesRemoves attribute from object instancedel obj.attr

1. Syntax and Overview 🔝

del target

The target can be a variable name, a subscription (index/key), or an attribute reference. Multiple targets can also be deleted in a single statement (e.g., del a, b, c).

2. Deleting Variables 🔝

When you delete a variable using del, the variable name is removed from the current namespace. Attempting to access it afterward raises a NameError:

name = "Plus2Net"
print(name)

del name

try:
    print(name)
except NameError as e:
    print("Caught error:", e)

assert 'name' not in locals()

3. Deleting List Items and Slices 🔝

You can remove specific elements from a list by index or delete an entire range of items using slices:

numbers = [10, 20, 30, 40, 50]

# Delete single item by index
del numbers[0]
print(numbers)  # [20, 30, 40, 50]

# Delete slice (remove elements from index 1 to 3)
del numbers[1:3]
print(numbers)  # [20, 50]

assert numbers == [20, 50]

4. Deleting Dictionary Keys 🔝

To remove a key-value pair from a dictionary, pass the key to del:

student = {'id': 1, 'name': 'John', 'mark': 85}

del student['mark']
print(student)  # {'id': 1, 'name': 'John'}

assert 'mark' not in student

Frequently Asked Questions (FAQ) 🔝

What is the difference between `del`, `list.pop()`, and `list.remove()`?

del is a statement that removes an item by index/slice or deletes a variable reference without returning any value. pop() removes an item by index and returns its value. remove() searches for a specific value and removes its first occurrence.

Does `del` delete the object from memory immediately?

del removes the variable name reference from the namespace. The underlying object is deleted from memory (garbage collected) only if its reference count drops to zero (i.e., no other variables point to it).

What happens if I try to delete a non-existent dictionary key or variable?

Python will raise a KeyError if the dictionary key does not exist, or a NameError if the variable name is not defined in the current scope.

Further Reading & Related Tutorials 🔝

Practice in Google Colab 🔝

Test variable unbinding, list item deletion, slice removal, and dictionary key clearing in our interactive Google Colab companion notebook:

Interactive Practice: Colab & GitHub

Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:

View on GitHub Open In Colab



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer