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 Playground

Tags

languagesyntaxcorememory

Related Items