Python is a prominent high-level programming language used for web development, machine learning, artificial intelligence and many more. It is a versatile language and it becomes very easy for the programmers who already have a background in coding.
When we talk about coding in Python, del
is one of the most used Python keywords. It helps to delete objects, indexes or variables. This python keyword is used to delete the specified object and its reference counts. Once an object is deleted we cannot use its methods or attributes.
In this article, we will discuss del
keyword with a description, its usage and various examples.
Description
del
is one of the reserved keywords or a keyword in Python which helps in deleting objects, variables and indexes. del
statement abbreviates delete and is used to completely remove an object from memory.
The del statement also deletes a variable reference, not the actual value that the variable refers to. After using the del
statement, the variable is deleted from memory.
Usage
The syntax for the del
keyword is as follows –
del object
The object can be,
- A variable
- List items
- Slice items
Examples
Example 1: Deleting a variable
In this example, we are going to delete a variable “a” using the del
keyword using the following code –
a = "Del Keyword in Python" print(a) del a print(a)
Output:
Del Keyword in Python
Traceback (most recent call last): File "main.py", line 9, in print(a) NameError: name 'a' is not defined
Explanation:
We first declare the variable a
with a value "Del Keyword in Python"
. We then print the value of the variable using the print()
function. Lastly, we use the del
keyword to delete the variable. When we try to print the variable a
again after it has been deleted, Python raises the NameError
indicating that the name ‘a’ is not defined. This is because it cannot find the variable a
as it was deleted.
Example 2: Deleting an object in a list
In this example, we will delete an object from a list using the del
keyword.
my_list = ["apple", "banana", "cherry"] print(my_list) del my_list[1] print(my_list)
Output:
['apple', 'banana', 'cherry']
['apple', 'cherry']
Explanation:
We first create a list my_list
and print its contents using the print()
function. We then use the del
keyword to delete the object at index 1. Finally, we print the new contents of my_list
using the print()
function. As shown in the output above, the second item in the list (which was “banana”) has been deleted.
Example 3: Deleting a slice in a list
In this example, we will delete a slice from a list using the del
keyword.
my_list = ["apple", "banana", "cherry", "orange"] print(my_list) del my_list[1:3] print(my_list)
Output:
['apple', 'banana', 'cherry', 'orange']
['apple', 'orange']
Explanation:
We first create a list my_list
and print its contents using the print()
function. We then use the del
keyword to delete the slice from index 1 to index 2. Finally, we print the new contents of my_list
using the print()
function. As shown in the output above, the items “banana” and “cherry” have been deleted.
Example 4: Deleting an attribute of an object
In this example, we will delete an attribute of an object using the del
keyword.
class Employee: def __init__(self, name, age, location): self.name = name self.age = age self.location = location employee_1 = Employee("John", 28, "California") print(employee_1.location) del employee_1.location print(employee_1.location)
Output:
California
AttributeError: 'Employee' object has no attribute 'location'
Explanation:
We create a class named Employee
with attributes name, age and location. We create an instance of the class named employee_1
with the arguments specified. We print the value of the attribute location of employee_1
using the print()
function. We then use the del
keyword to delete the attribute location of the employee_1
instance. When we try to print the attribute location again, Python raises the AttributeError
which indicates that the given object does not have the specified attribute.
Conclusion
In this article, we went through the description, usage and various examples of the del
keyword in Python. We learned how to delete objects, variables, indexes and attributes of objects. Knowing how to use the del
statement is an important concept for managing memory and avoiding unnecessary memory leaks.