In this post, we’ll learn how to properly use Python del keyword. We’ll be applying this del keyword to Python objects, lists, tuples etc. and see what changes will be made to them.
We’ll be using multiple examples to explain Python del keyword in detail. So let’s not wait anymore and just get right into its implementation phase.
What is Python Del Keyword?
It is used to delete objects. These can be Python lists, tuples, a used-defined objects etc.
Let’s first see its syntax, then we’ll use it in practical examples.
Syntax of Del Keyword
del objectName
Its simple, we have to write the del keyword before the object. This object name can be a list, variable, user-defined object etc.
Implementing Python Del Keyword (Multiple Examples)
Below examples will show you how to properly and easily use this del keyword in multiple situations.
Example 1: Delete Variable using Del
colorName='green' idNumber=45 number=32.6 //before applying del print(colorName) // green print(idNumber) // 45 print(number) // 32.6 del colorName del idNumber del number //after applying del print(colorName) // NameError: name 'colorName' is not defined print(idNumber) // NameError: name 'idNumber' is not defined print(number) // NameError: name 'number' is not defined
We have defined 3 variables of integer, string and float data type. They are all printed successfully. But after we apply del on them and try to print their values, we get a name error.
The variables are completely removed which satisfies the condition that the Python del keyword is working properly.
Example 2: Delete List, Tuple, Dictionary and Set
We can apply the same process to delete them as well. See below code:
itemsList=['green',45,7.3,[4,2]] itemsSet={2,4,67,'red',22.6} itemsDictionary={'name':'Yasir','age':25} itemsTuple=(2,3,5,6,7,'blue',4.6) del itemsList del itemsSet // applying del on all of them del itemsDictionary del itemsTuple print(itemsList) print(itemsSet) // name error for all of them print(itemsDictionary) print(itemsTuple)
You can see that the Python del keyword has successfully deleted all these items.
In tuple, you cannot delete a specific item as tuple does not support item deletion. That means you cannot use del tuplename[4] like this. But you can delete the entire tuple as specified in the above code.
Example 3: Remove Items from List Using Del
If you apply the del keyword on list then it will delete the whole list and printing the name will give a name error. But you can specify items inside list to be removed/deleted. See below code examples:
itemsList=['green',45,7.3,[4,2]] del itemsList[3] // delete the 4th item of list as index starts from 0 to list length -1 print(itemsList) // ['green', 45, 7.3]
The 4th item is deleted from the list using the del keyword. This is how you can specify which item to delete from the list.
If you want to delete a specific range of items from list then use the list slice method. See below:
itemsList=['green',45,7.3,[4,2],5,4,3,65] del itemsList[1:5] // will delete the range of items from 2nd to 5th print(itemsList) // ['green', 4, 3, 65]
You can see that the specified range of items are deleted from this list. If you want to delete all items from list then use this:
del listName[:] // it will print []
All the items of list will be removed by using the above code/
Example 4: Delete Key Value Pair From Dictionary Using Del
itemsDictionary={'name':'Yasir','age':25,'height':6} print(itemsDictionary) // {'name': 'Yasir', 'age': 25, 'height': 6} del itemsDictionary['age'] // it will delete the age key which will remove the value as well print(itemsDictionary) // {'name': 'Yasir', 'height': 6}
You can delete any specific key and value pair of your Python dictionary using del keyword with this method.
So this is how you can use Python del in your own code. Hope you now have a complete understanding of how to use it.
Do ask if you still have questions related to the usage of Python del keyword. I will be glad to answer all.
Conclusion
To conclude, hope you now have an in-depth understanding of how and where to use Python del keyword. Your valuable feedback would be well appreciated. Thank you for reading this post.