Python

Python Del Keyword

Just like C++, Python is also object-oriented programming as everything within it has been taken as an object. We can perform different functions on the objects like lists, dictionaries, tuples, and variables. Python has given us the keyword “del” to delete a single object, and it’s quite easy to do in a single step. In today’s guide, we will discuss python’s “del” keyword and its usage on different sorts of objects using different ways. This article will be a great bonus if you are new to the “del” concept. Let’s start today’s article by opening the shell. Make sure that your Linux distribution has python’s latest version installed. If you don’t have one, get install one by the shown-below query in the image.

We need a python file to make code in Linux. Let’s get started with making a new python file with the shown-below instruction. You can try using the text editor, vim editor, or nano editor to open this new file, just like we did in the image beneath.

Example 01:

The file will be empty when we open it first. To start the code, we must put the python support in our file, i.e. “#!/usr/bin/python”. We have started this code by initialising a string variable “x” with the value “Ubuntu”. The very next print statement is here to present the value of variable “x” on the shell. Here comes the main part. We have used the “del” keyword with the variable “x” to delete the object “x”. The next print statement has been used to display the value of “x” after deleting it. We expect this line to display an error because the object “x” has been deleted already using the “del” keyword. Save this code and exit the file with Ctrl+X.

We ran this code with python3 support using the file name below. It shows the value of “x” as “Ubuntu” by using the print statement used before the “del” keyword. After using the “del” keyword, the object “x” has been deleted. This is why the use of print statements to display object “x” after it causes the “NameError” on execution.

We have opened the same file to update the code again. We have substituted the variable “x” with a tuple “t”. We have assigned this tuple 2 string values and 2 numerical values. The print declaration is used to display the whole tuple. Then, we have deleted this tuple using “del” and used the “print” statement again to display the tuple, which will show NameError.

The print statement has displayed the tuple on the execution of the updated python code. But the use of “del” before the next print statement shows us the NameError for the tuple “t”.

Example 02:

Let’s take a look at the use of the “del” keyword within a new example. This time, we have been taking a list object having 4 string values in it. The print statement will display this list on the shell screen. Here comes the “del” part. We have used the “del” keyword and the index of a list to delete the value at a particular index. Let’s say we have used index 3 to delete the last value from the list. The print statement will now show the updated list. We have maintained the sequence of using the indexes in descending order to delete the value from a list. The descending order will delete values from the last index of a value, and hence there will be no error upon the use of the “del” keyword. We have done it for up to 0 indexes of a list.

We have got the full list displayed on the first line of output. After that, each value from the end of the list is removed at every new output line. In the end, the empty is displayed.

When we use the ascending order to delete values from the list, it will not give errors when the list shrinks. For instance, in the below code, upon using the “del” keyword for deleting a value at “0”, the index will shrink the list, and index 3 will be no more. After using the “1” index, there will be no more index 2. After using the “2” index, we will get an error.

Running this code, executed the first 3 print statements and deleted values “Ubuntu” and “Windows” from the list. After that, it throws an error as the list has been shrunk.

To avoid this error, we have to only use the index “0” to delete the values.

This will delete each value from the start every time, and the list will be empty at last, as below.

Example 03:

Let’s take an example of a “del” keyword to delete the value from a list of lists. We have initialized a list “ls” having three lists in it. The print statement displays the whole list. After this, we have used the “del” keyword to delete the value at index “1” of each list. 0, 1, 2 represents the list number. After using the print statement, It will be displayed with the remaining values.

The output shows the new list of lists left with single values.

If you want to delete values from specific list indexes, you have to use the shown-below deletion method. We have printed the list and used the “del” keyword to delete values at indexes 1, 2, and 3 from a list. To specify the indexes, use [] and mention the index numbers like 1:4. The index “4” will be excluded here. The last print statement will display the updated list.

This code execution shows that the value: Linux, Windows, OS has been removed from the list.

Example 04:

The dictionary object stores its value in a key-pair format. So, we have initialized a dictionary having 3 key-pair values. The del keyword uses the “key” name to delete the specific value from the dictionary, and print statements are used to show the before and after dictionary.

The result shows the original dictionary and the updated dictionary with 2 values.

We have created a class “test” with 3 variables. Created an object “t” to call the class variables in the first 2 print statements. The “del” keyword is utilized to delete the object “t” and use the print statement to call the last variable with object “t”.

We have got 2 variable values displayed before using “del” on the execution of this code. After deleting the object “t”, we have got an error using the last print statement.

Conclusion:

This is all about using the “del” keyword to delete different objects from the python code used in the Ubuntu 20.04 system. We have used the “del” keyword in different ways on most used objects of python like lists, tuples, dictionaries, variables, and classes. All the examples used in this article are equally usable at any other python platform to get the same outputs.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.