Skip to content
Foxipa

Suggest an edit

Tell us what should be corrected, clarified, or improved. Submission will be connected to Foxipa’s contribution service later.

No information is sent anywhere yet. Your suggestion is only prepared in your browser.

Learning

Python del Statements

Learn how Python's del statement removes name bindings, items, slices, and attributes, and why deleting a binding is not the same as destroying an object.

Levels: Intermediate ~16 min read

Intermediate

Python's del statement removes a target rather than calling a method. What it removes depends on the target: a name binding, an item, a slice, or an attribute. It is a Core Python statement, not a special list-only operation.

Delete a name binding

When you delete a name, Python removes that name's binding from the relevant namespace. The value may still exist if something else refers to it. Trying to use the deleted name before rebinding it raises NameError.

python name.py
1
2
3
4
5
6
7
8
9
draft = "version 1"
print(draft)

del draft

try:
    print(draft)
except NameError as error:
    print(type(error).__name__)

The name is removed from the namespace; a later reference fails until the name is bound again.

Delete a list item

A subscription target can be deleted. For a list, the item at that index is removed and the later items shift left. Unlike list.pop() this statement does not return the removed item.

python item.py
1
2
3
4
letters = ["a", "b", "c", "d"]
del letters[1]

print(letters)

Deleting an indexed list target removes that item from the list.

Delete a slice

A slice can also be a deletion target. The selected range is removed, so the list can become shorter by more than one item. An empty slice target clears the selected range without creating a replacement value.

python slice.py
1
2
3
4
numbers = [10, 20, 30, 40, 50]
del numbers[1:4]

print(numbers)

The slice target identifies the range that is removed.

Delete an attribute

An attribute reference can be a deletion target too. The object is responsible for carrying out the deletion of that attribute.

python attribute.py
1
2
3
4
5
6
7
8
9
class Profile:
    pass

profile = Profile()
profile.name = "Ada"
print(profile.name)

del profile.name
print(hasattr(profile, "name"))

The attribute is removed from this instance, so the later hasattr check is false.

Deleting a binding is not the same as destroying an object

A del statement removes the target's binding. It does not mean that the underlying object must immediately disappear. Another binding can still refer to the same object.

python binding.py
1
2
3
4
5
6
7
items = ["red", "green"]
alias = items

del items
alias.append("blue")

print(alias)

The object remains reachable through alias after the items name is deleted.

Delete multiple targets

A target list can contain several targets. Python deletes them from left to right, using the same target rules for each one.

python multiple.py
1
2
3
4
5
6
7
8
9
first = "A"
second = "B"
third = "C"

del first, second, third

print("first" in locals())
print("second" in locals())
print("third" in locals())

All three name bindings are removed by one del statement.

Deleting an unbound target raises NameError

A name must be bound before it can be deleted. Deleting an unbound name raises NameError. This is different from deleting an existing item or attribute when the target object has its own deletion rules.

python unbound.py
1
2
3
4
try:
    del missing_name
except NameError as error:
    print(type(error).__name__)

An unbound name cannot be deleted, so Python raises NameError.

Choose deletion deliberately

Use del when you specifically need deletion semantics: remove a name binding, a selected item or slice, or an attribute. For collection-specific operations that return a removed value or express a different intent, use the collection's own method instead. Keeping the operation that matches the intent makes code easier to read.

Mental model

Think of del as removing a target's connection to an object. The exact effect depends on the target, but the statement itself does not mean "destroy this object now."

Was this page helpful?

Mark this learning resource complete to track your learning progress.