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 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().
| Usage | Description | Example |
|---|---|---|
| Variables | Removes name binding from namespace | del x |
| Lists | Removes item by index or slice | del my_list[0] |
| Dictionaries | Removes key-value pair by key | del my_dict['key'] |
| Attributes | Removes attribute from object instance | del obj.attr |
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).
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()
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]
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
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.
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).
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.
Test variable unbinding, list item deletion, slice removal, and dictionary key clearing in our interactive Google Colab companion notebook:
Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:
View on GitHubAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.