Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Const

del

del removes a name binding or deletes an item, attribute, or slice. It acts on targets and does not return a value.

How it works

The del statement removes bindings, items, attributes, or slices rather than producing a value. Its effect is delegated to the affected object for attributes and subscriptions, while deleting a name removes that name from the applicable namespace.

Syntax

del target_list

Key points

  • A comma-separated target list is processed recursively in left-to-right order.
  • Deleting a variable removes its namespace binding rather than assigning a replacement value.
  • A global declaration determines whether name deletion targets the global namespace.
  • Deleting an attribute or indexed item delegates the operation to the object being accessed.

Common mistakes

  • Deleting a name that has no binding raises NameError.
  • Deleting a slice generally behaves like assigning an empty slice, but the target object determines the exact behavior.

Example: Remove targets

items = ["a", "b", "c", "d"]
del items[1:3]
print(items)  # ['a', 'd']

name = "temporary"
del name
print("name" not in locals())  # True

The statement removes a list slice and then removes a local name.

When to use it

Discard a name

Remove a variable binding that should no longer be available in the current namespace.

Edit sequences

Remove one or more elements from a mutable sequence by index or slice.

Delegate deletion

Remove a mapping entry or object attribute through the target object's deletion protocol.

Interview questions

QWhat is the syntax and general behavior of del?

The syntax is del target_list. It can delete names, attributes, subscriptions, and slices; a target list is processed from left to right.

Follow-up: What happens with del x inside a function? The name is removed from the local namespace unless a global declaration makes it refer to the global namespace.

QWhat happens when a name is deleted?

Deleting a name removes its binding from the local or global namespace. Attempting to delete a name that is not bound raises NameError.

Follow-up: Does deleting a name necessarily destroy its object? No; it removes one binding, while the object can remain reachable through other references.

QHow does del work with attributes, subscriptions, and slices?

For an attribute or subscription, the operation is passed to the primary object, so the object's deletion behavior controls the result. Slice deletion is generally like assigning an empty slice of the appropriate type.

Follow-up: Why can slice deletion vary between objects? The documentation says the sliced object determines even the empty-slice behavior.

  • assignment statement
  • global statement
  • target list
  • attribute references
  • subscriptions
  • slicing

Source: 7. Simple statements. Checked 2026-10-16.