Python del
Keyword
The del
keyword in Python is used to delete variables, list elements, dictionary keys, or even entire objects. It helps free memory and remove unwanted elements from data structures.
Syntax
</>
Copy
del object_name
Usage of del
The del
keyword can be used in different contexts:
- Deleting variables
- Removing elements from lists
- Deleting dictionary keys
- Deleting entire lists, dictionaries, or objects
Examples
1. Deleting a Variable
Here, we create a variable and then delete it using del
. After deletion, trying to access the variable will result in an error.
</>
Copy
# Define a variable
x = 10
print("Value of x before deletion:", x)
# Delete the variable
del x
# Trying to access x will result in an error
try:
print(x)
except NameError:
print("Variable x is deleted and no longer accessible.")
Output:
Value of x before deletion: 10
Variable x is deleted and no longer accessible.
2. Deleting an Element from a List
We can use del
to remove a specific element from a list by specifying its index.
</>
Copy
# Define a list
numbers = [10, 20, 30, 40, 50]
print("Original list:", numbers)
# Delete element at index 2 (third element)
del numbers[2]
print("List after deleting element at index 2:", numbers)
Output:
Original list: [10, 20, 30, 40, 50]
List after deleting element at index 2: [10, 20, 40, 50]
3. Deleting a Slice from a List
We can delete multiple elements using slicing.
</>
Copy
# Define a list
numbers = [1, 2, 3, 4, 5, 6]
print("Original list:", numbers)
# Delete a slice (removing elements at index 1 to 3)
del numbers[1:4]
print("List after deleting slice:", numbers)
Output:
Original list: [1, 2, 3, 4, 5, 6]
List after deleting slice: [1, 5, 6]
4. Deleting a Dictionary Key
We can delete a specific key-value pair from a dictionary using del
.
</>
Copy
# Define a dictionary
person = {"name": "Arjun", "age": 25, "city": "New York"}
print("Original dictionary:", person)
# Delete the 'age' key
del person["age"]
print("Dictionary after deleting 'age' key:", person)
Output:
Original dictionary: {'name': 'Arjun', 'age': 25, 'city': 'New York'}
Dictionary after deleting 'age' key: {'name': 'Alice', 'city': 'New York'}
5. Deleting an Entire List or Dictionary
We can delete an entire list or dictionary using del
. Once deleted, trying to access the list or dictionary will cause an error.
</>
Copy
# Define a list
fruits = ["apple", "banana", "cherry"]
print("List before deletion:", fruits)
# Delete the entire list
del fruits
# Trying to access the list will result in an error
try:
print(fruits)
except NameError:
print("The list 'fruits' has been deleted.")
Output:
List before deletion: ['apple', 'banana', 'cherry']
The list 'fruits' has been deleted.
6. Deleting an Object
The del
keyword can also delete objects of a class.
</>
Copy
# Define a class
class Car:
def __init__(self, brand):
self.brand = brand
# Create an object of Car
my_car = Car("Toyota")
print("Car brand:", my_car.brand)
# Delete the object
del my_car
# Trying to access my_car will result in an error
try:
print(my_car.brand)
except NameError:
print("The object 'my_car' has been deleted.")
Output:
Car brand: Toyota
The object 'my_car' has been deleted.