Data Model

Delete Statements

del removes bindings, items, and attributes rather than producing a value.

del removes a binding or an item. It is a statement, not a function, and it does not return the removed value.

Use del name when a name should no longer be bound. Use del mapping[key] or del sequence[index] when mutating a container by removing one part.

This is different from assigning None: None is still a value, while del removes the binding or slot.

Deleting a dictionary key mutates the dictionary. The key is gone; it has not been set to None.

Source

profile = {"name": "Ada", "temporary": True}
del profile["temporary"]
print(profile)

Output

{'name': 'Ada'}
del x removes the name; the object survives if any other reference holds it, otherwise gets collected.

Deleting a list item removes that position and shifts later items left.

Source

items = ["a", "b", "c"]
del items[1]
print(items)

Output

['a', 'c']

Deleting a name removes the binding from the current namespace. It is different from rebinding the name to None.

Source

value = "cached"
del value
print("value" in locals())

Output

False

Notes

See also

Run the complete example

Example code

Expected output

{'name': 'Ada'}
['a', 'c']
False

Execution time appears here after you run the example.