Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Python del Keyword



The Python, del keyword is used to delete an object. It is a case-sensitive keyword. It is used only on mutable elements [list, dictionary, sets]. Mutable elements are those which can be changed after creation. When we try to delete an immutable element it will result in TypeError.

Syntax

Following is the syntax of the del keyword −

del object_name

Example

Following is the basic example of Python del keyword −

var1 = 13
del var1
print(var1)

Output

Following is the output of the above code −

Traceback (most recent call last):
  File "/home/cg/root/37689/main.py", line 3, in <module>
    print(var1)
NameError: name 'var1' is not defined. Did you mean: 'vars'?

Using 'del' Keyword in List

The del keyword can be used in list for deletion of entire list or any particular index value or through slicing method from a list.

Example

Following is an example −

list1 = [12, 34, 100, 56, 89, 250, 45, 465]
# deleting 2nd value
del list1[2]
print("List :",list1)
# deleting values from 5th to 7th 
del list1[5:7]
print("List :",list1)
# deleting all elements
del list1
print(list1)

Output

Following is the output of the above code −

List : [12, 34, 56, 89, 250, 45, 465]
List : [12, 34, 56, 89, 250]
Traceback (most recent call last):
  File "E:\pgms\Keywords\del.py", line 23, in <module>
    print(list1)
          ^^^^^
NameError: name 'list1' is not defined. Did you mean: 'list'?

Using 'del' Keyword in Dictionary

We can also use del keyword in dictionaries to delete an item from it. We can delete a single item from the dictionary or the entire dictionary.

Example

Here, we have created dictionary,dic1 and deleted 'Age' key from it −

dic1 = {'Name':'Raja', 'Age':25, 'Language':'Python'}
#deleting 2nd element
del dic1['Age']
print("dic1 :",dic1)

Output

Following is the output of the above code −

dic1 : {'Name': 'Raja', 'Language': 'Python'}

Using 'del' in Tuple

We cannot delete an items of tuple using del keyword, because it is an immutable element. Immutable elements are those which cannot be changed after creation. When we try to delete an item from the tuple it will result in TypeError.

Example

Here, we have created a tuple,Tup1 and when we tried to delete an item from it, an error occurred −

Tup1 = ('Python', 'Java', 'C++', 'HTML')
del Tup1[2]
print(Tup1)

Output

Following is the output of the above code −

Traceback (most recent call last):
  File "/home/cg/root/15524/main.py", line 2, in <module>
    del Tup1[2]
TypeError: 'tuple' object doesn't support item deletion

Using 'del' Keyword in class

We can also delete a class using del keyword −

Example

Here, we have created a class and deleted is using del keyword −

class Tutorialspoint:
    def Tp():
        print("Welcome to Tutorialspoint")

print(Tutorialspoint)
del Tutorialspoint
print(Tutorialspoint)

Output

Following is the output of the above code −

<class '__main__.Tutorialspoint'>
Traceback (most recent call last):
  File "E:\pgms\Keywords\del.py", line 65, in <module>
    print(Tutorialspoint)
          ^^^^^^^^^^^^^^
NameError: name 'Tutorialspoint' is not defined
python_keywords.htm
Advertisements