How to Use Python del Statement?
Python is also an object-oriented programming language just like C++ because everything within it is an object. We can perform various functions on objects such as lists, dictionaries, tuples, and variables.
In Python, the programmer can delete any variable, function, data structure, or user-defined object. After a programmer has defined these iterable objects, their existence in the program can be eliminated.
There are numerous reasons for doing this, including saving memory space, increasing compilation time, increasing coding efficiency, reusing object names with a new set of data values, etc.
Python includes a keyword that allows the user to do such functions. The del keyword in Python is used to delete any element or object from a program.
The Python del statement can be used in a variety of contexts, such as -
- To delete a variable name from both the local and global scopes.
- To delete an item or a data value from a list, set, dictionary, or any other mutable iterable in Python.
- Attributes of the class instances can be deleted.
- To delete a slicing from an iterable list.
Syntax:
Parameter | Description |
---|---|
del | Keyword in python used to delete an object. |
object_name | Name of the iterable object, which can be a list, tuple, dict, variable, user-defined object, etc. |
Note: The del keyword can be used to delete an item from a list by referring to its index rather than its value.
a) Using Python Del to Delete a User-Defined Object
Example: In this example, we have created a class that outputs the value of a variable. The first print statement displays all the values of the class variables. After that, we have used the del statement to delete the existence of this class. When we use the print statement again with the same class name, the Python compiler throws an error.
Output of the above code:
b) Using Python Del to Delete Dictionary, List, and Variable
Example: In this example, we have created a variable, list, tuple, and dictionary and passed values in them. After that, we have used the del keyword to delete their existence. When we use the print statement with the name, car_list, my_tuple and my_dict the Python compiler throws an error.
Output of the above code:
c) Removing Items and Slices from a List Using Python Del
The Python del statement can also be used to delete a specific element from an iterable object or a range of data values from the iterable. This can be achieved by using the Indexing method. To delete certain data values you have to provide the index values.
Example: In this example, we have created the list and passed values in it. After that, we have deleted a specific value, slice from the list, and lastly the entire list.
Output of the above code:
d) Using Python Del to Remove Key: Value Pair From A Dictionary
Python del can be used to delete a specific key-value pair from a dictionary. The del command only accepts the key as an index value when deleting a specific data element from the dictionary. This can be achieved using the below method.
Example: In this example, we have created a dictionary named Employee and passed values in it. After that, we deleted a key-value pair from the dictionary using the del keyword and passed the print statement to the old and updated dictionary.
Output of the above code:
Python Del with Tuples and Strings
Example: In this example, we are going to create a tuple and pass the value in it. After that, we will delete the entire tuple.
Output
Note: Single items and values within a tuple or string cannot be deleted. Tuples and strings are immutable objects, which means they cannot be changed after they have been formed. However, we can delete an entire tuple or a string.
Output
FAQs
Q: What is the Purpose of the Del Keyword in Python?
A: The del keyword in Python is used to delete variables and objects from a Python program. Using the del keyword, iterable objects such as user-defined objects, lists, sets, tuples, dictionaries, user-defined variables, etc can be deleted from existence and memory locations. The del statement is used for two reasons: The first is to remove elements from dicts and lists by index; you can also delete a slice from lists. The second reason is that you want to unbind a variable. Changing the value of a variable to None has no greater impact on memory use than deleting it.
Q: What is Del in Python?
A: The del() method is known as a destructor method in Python. It is called when an object's garbage collection occurs, which occurs after all references to the object have been deleted.
Output of the above code:
Q: Why is it Necessary to Delete Variables?
A: Memory management is an important consideration aspect when developing software in Python or any programming language. It comes into consideration when you are dealing with complex and large lines of code. While writing any software, you don't even see how it affects your code.
This is because you haven't yet dealt with large software working on large amounts of data, sometimes known as big data. When working with large amounts of data, you'll notice how memory management affects the overall efficiency and processing time of your software.
Learn More
Conclusion
- Python treats everything as an object, and practically everything has attributes and functions. Thus, the primary goal of Python del is to delete objects in the Python programming language. Objects in this context can be dictionaries, variables, lists, or parts of lists, etc.
- There are numerous reasons for doing this, including saving memory space, increasing compilation time, increasing coding efficiency, reusing object names with a new set of data values, etc.
- Single items and values within a tuple or string cannot be deleted. Tuples and strings are immutable objects, which means they cannot be changed after they have been formed. However, we can delete an entire tuple or a string.