del

del removes a binding — a name from a namespace, an item or slice from a list or dict, an attribute from an object. It never destroys an object other references still point to.

DefinitionsPython 3 (all)Live demo
name
del x
item / key
del nums[0]
del d['key']
slice
del nums[1:3]
del nums[::2]
attribute
del obj.attr
several
del a, b[0], c.x
Use for
del d[key] / del items[i] / del items[a:b] / del obj.attr
Result
a statement — no value; the name or item is gone afterwards
Pairs with
list.pop(), dict.pop(), list.remove(), delattr()
Watch out
del x unbinds a name, it does not free the object; deleting by index while looping skips items

Demo

Live evaluation
Remove one item by position. Negative indexes count from the end; a missing position raises.
Try:
Inputs
iint-4 … 3 are valid
Code
nums = [10, 20, 30, 40]
del nums[0]
nums
Result
[20, 30, 40]

Compare the first two tabs: del nums[4] raises IndexError: list assignment index out of range (deletion reports as an assignment), while the slice 4:2 or -2:99 never raises — out-of-range slice bounds are clamped, and an empty range deletes nothing. A float bound (type 1e21 or more) is a TypeError in both. In the del name tab, the object is untouched; only the name a is gone from the namespace.

Syntax slots

NameTypeRequiredDescription
targetname / subscription / attributeyesSame forms as the left side of an assignment. A comma-separated list deletes each target left to right.

Common patterns

Drop a dict key if present
del raises KeyError for a missing key; pop with a default does not.
if 'password' in user:
    del user['password']
# or: user.pop('password', None)
Truncate a list in place
Keeps the same list object, so other references see the change.
del history[100:]
Clear a list in place
Same as items.clear().
del items[:]
Free a big temporary early
Drops this reference; the memory is reclaimed only if nothing else refers to the object.
frame = load_huge_frame()
summary = frame.describe()
del frame

Examples

1. A deleted name is gone
x = 10 del x x
Returns
NameError: name 'x' is not defined
2. Delete a list item
nums = [1, 2, 3] del nums[0] nums
Returns
[2, 3]
3. Delete a dict key
d = {'a': 1, 'b': 2} del d['a'] d
Returns
{'b': 2}
4. Delete every other item
nums = list(range(10)) del nums[::2] nums
Returns
[1, 3, 5, 7, 9]
5. Delete an attribute
class User: pass u = User() u.token = 'abc' del u.token hasattr(u, 'token')
Returns
False
6. Several targets at once
a = b = c = 0 del a, c [n for n in ('a', 'b', 'c') if n in globals()]
Returns
['b']
7. A deleted local
def f(): x = 1 del x return x f()
Returns
UnboundLocalError: cannot access local variable 'x' where it is not associated with a value
8. Tuples and strings are immutable
t = (1, 2) del t[0]
Returns
TypeError: 'tuple' object doesn't support item deletion

Pitfalls

1. Deleting by index while looping over the same list
Each del shifts the rest left: the next item is skipped, and range(len(...)) — fixed before the loop — runs past the new end.
del inside the loop
nums = [1, 2, 3, 4]
for i in range(len(nums)):
    if nums[i] % 2 == 0:
        del nums[i]
IndexError: list index out of range
build a new list
nums = [1, 2, 3, 4]
nums = [n for n in nums if n % 2 != 0]
nums
[1, 3]
2. Expecting del to destroy the object
del removes one name. Every other reference still sees the object; to empty it for everyone, mutate it.
del the name
data = [1, 2, 3]
cache = {'data': data}
del data
cache['data']
[1, 2, 3]
clear the object
data = [1, 2, 3]
cache = {'data': data}
data.clear()
cache['data']
[]
3. del by position when you meant by value
del nums[2] removes whatever is at index 2. To remove the value 2, use remove().
index 2
nums = [5, 2, 9]
del nums[2]
nums
[5, 2]
value 2
nums = [5, 2, 9]
nums.remove(2)
nums
[5, 9]

When to use

Use it
  • Removing a dict key or list item when you do not need the removed value
  • Removing a range of a list in place (del items[a:b])
  • Dropping a large temporary so its memory can be reclaimed sooner
Reach for something else
  • You need the removed value → list.pop(i) / dict.pop(key)
  • Removing by value → list.remove(x)
  • Filtering many items → a comprehension builds the result in one pass
  • Attribute name held in a string → delattr(obj, name)

Notes

CPython impl
del x decrements the object’s reference count; CPython frees the object immediately only when that count reaches zero (and no reference cycle keeps it alive)
Scope
del name inside a function makes name local to the whole function, just like an assignment — reading it afterwards is UnboundLocalError, not NameError
Protocols
del obj[k] calls type(obj).__delitem__(obj, k); del obj.a calls __delattr__ (or a property deleter)

FAQ

No — it deletes a reference (a name, an item slot, an attribute). The object is freed only when nothing refers to it any more. If another variable, list or dict still holds it, it stays alive.

History

3.2
Deleting a local name that a nested function uses as a free variable became legal.