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.
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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."
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.