del
KeywordPython 2.0+Intermediate
Deletes a variable, list item, dictionary entry, or object attribute
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.0+
Learn by Difficulty
Quick Example
python
# Delete a variable x = 42 print(x) del x try: print(x) except NameError as e: print(f"Error: {e}") # Delete list items numbers = [1, 2, 3, 4, 5] del numbers[2] # remove index 2 print(numbers) # Delete dict keys person = {"name": "Alice", "age": 30, "temp": True} del person["temp"] print(person)
del removes a name binding, list element, dict key, or attribute. It does not directly free memory — that's handled by garbage collection.
Try in PlaygroundTags
languagesyntaxcorememory
Related Items
__del__
Dunder Method
Destructor; called when the object is about to be garbage collected
__delitem__
Dunder Method
Enables item deletion with del obj[key]
__delattr__
Dunder Method
Called when deleting an attribute with del
gc
Stdlib — Introspection
Garbage collector interface: enable/disable, find reference cycles