Python is a high-level, interpreted programming language and is one of the most popular programming languages present in the software industry.
One of the most important but often overlooked concepts in python is that of the del statement. Let us have a look at it in detail.
In python, del statements are basically used to delete objects that are no longer required in order to free up memory space.
In python, everything is represented in the form of an object, so a del statement can be used to delete anything ranging from a variable to a list, string, etc.
One important thing to remember is that no value is returned by del statements.
The syntax of del statements is very straightforward and is as follows.
We simply use the keyword del followed by the name of the object to be deleted.
del <object_name>
As mentioned before, we can use the del statement to delete a variable to free up space in the memory.
We use the del keyword followed by the name of the variable to be deleted.
In this example, we consider a variable named a and first assign a value of 2 to it.
In the first print statement the value of a, that is 2, is printed but when we try to print it after deleting it, we get an error.
Let us look at the python code and corresponding output for this example:
# Creating a variable
a = 2
# Printing value
print(a)
# Deleting the variable
del a
# Trying to print
print(a)
Output
2
NameError: name 'a' is not defined
We use the del keyword followed by the name of the object representing the string to be deleted.
Here we have a string Hello stored in a variable named a and have deleted it.
Note that we can only delete the entire string and not individual characters like we can in a list shown below.
Let us look at the python code and corresponding output for this example:
# Creating a string
a = "Hello"
# Printing value
print(a)
# Deleting the variable
del a
# Trying to print
print(a)
Output
Hello
NameError: name 'a' is not defined
We use the del keyword followed by the name of the object representing the list to be deleted.
Here, we have a list with three elements 0, 1, and 2 stored in a variable named a and have deleted it.
Let us look at the python code and corresponding output for this example:
# Creating a list
a = [0, 1, 2]
# Printing value
print(a)
# Deleting the list
del a
# Trying to print
print(a)
Output
[0, 1, 2]
NameError: name 'a' is not defined
Unlike strings, we can use the del statement to delete individual elements as well.
Here, we delete the element at index 1 that also happens to be 1. Only 0 and 2 are left in the list.
Let us look at the python code and corresponding output for this example:
# Creating a list
a = [0, 1, 2]
# Printing value
print(a)
# delete a single element of the list
del a[1] # passing index value
# Trying to print
print(a)
[0, 1, 2]
[0, 2]
We can use the del statement to delete a key-value pair from a dictionary to free up space in the memory.
We use the del keyword followed by the name of the object representing the item to be deleted.
Here, we have a list with three elements with keys as a, b and c and corresponding values 0, 1, and 2 stored in a variable named dict and have deleted the item that has key as b.
Let us look at the python code and corresponding output for this example:
# Creating a dictionary
a = {'a':0, 'b':1, 'c':2}
# Printing value
print(a)
# deleting a single entry of the dictionary
del a['b']
print(a)
# Deleting the entire dictionary
del a
# Trying to print
print(a)
Output
{'a': 0, 'b': 1, 'c': 2}
{'a': 0, 'c': 2}
NameError: name 'a' is not defined
In this topic, we have learned the use and advantages of del statements in a Python program, following some simple running examples, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.