del — Easy Examples
Deletes a variable, list item, dictionary entry, or object attribute
Deleting variables and items
Using del to remove names and collection elements.
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)
Expected Output
42
Error: name 'x' is not defined
[1, 2, 4, 5]
{'name': 'Alice', 'age': 30}del removes a name binding, list element, dict key, or attribute. It does not directly free memory — that's handled by garbage collection.
Want to try these examples interactively?
Open Easy Playground