Programming Search⌘K

Featurecore

The del statement

Also called del keyword, delete a variable, delete a list item, delete a dictionary key, del a slice, remove an attribute.

del removes bindings. del x unbinds a name, del a[i] removes an item, del a[i:j] removes a slice and del obj.attr removes an attribute. It does not destroy the object itself.

Language
Python

Synopsis

a = [-1, 1, 66.25, 333, 333, 1234.5]
del a[0]
print(a)
del a[2:4]
print(a)
del a[:]
print(a)
Output
[1, 66.25, 333, 333, 1234.5]
[1, 66.25, 1234.5]
[]

This is the example of the tutorial. del removes an item by its index, a slice, or all the items.

Semantics

r1. "Deletion of a target list recursively deletes each target, from left to right."1 Example: targets.

r2. "Deletion of a name removes the binding of that name from the local or global namespace, depending on whether the name occurs in a global statement in the same code block. Trying to delete an unbound name raises a NameError exception."1 Example: unbind.

r3. "Deletion of attribute references and subscriptions is passed to the primary object involved; deletion of a slicing is in general equivalent to assignment of an empty slice of the right type (but even this is determined by the sliced object)."1 Examples: list-items, targets.

r4. The del statement "differs from the pop() method which returns a value."2

r5. del x does not destroy the object. The data model says that "del x doesn’t directly call x.del()". The del statement "decrements the reference count for x by one", and __del__() "is only called when x’s reference count reaches zero."3 Example: other-name.

Examples

d = {"a": 1, "b": 2, "c": 3}
nums = [10, 20, 30]

class Box:
    pass

box = Box()
box.size = 5
del d["a"], nums[-1], box.size
print(d, nums, hasattr(box, "size"))
Output
{'b': 2, 'c': 3} [10, 20] False

The dictionary removes a key, the list removes an item and the object removes an attribute (rule r3).

This code raises NameError, on purpose.

x = 1
del x
print(x)
a = [1, 2]
b = a
del a
print(b)
Output
[1, 2]

del a removes only the name a. The list still exists, because b refers to it (rule r5).

Pitfalls

del on a missing key raises an error:

This code raises KeyError, on purpose.

d = {"a": 1}
del d["b"]

Use d.pop("b", None) if the key can be missing.

A del of an index in a loop over the same list skips items, because the items after the deleted one move to a lower index:

This code runs but gives a wrong answer on purpose.

nums = [1, 2, 2, 3]
for i, n in enumerate(nums):
    if n == 2:
        del nums[i]
print(nums)
Output
[1, 2, 3]

Build a new list, as in nums = [n for n in nums if n != 2].

Comparison

To remove a map entry, Go has a built-in function: "The built-in function delete removes the element with key k from a map m." Also, "If the map m is nil or the element m[k] does not exist, delete is a no-op."4 A Python del d[k] raises KeyError when the key does not exist, as in the missing-key example.

References

  1. The Python Language Reference, Python 3.15, 7.5. The del statement
  2. The Python Tutorial, 5. Data Structures, 5.2. The del statement
  3. The Python Language Reference, Python 3.15, 3.3.1. Basic customization, object.__del__()
  4. The Go Programming Language Specification, Deletion of map elements

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source