Python del to delete objects
The del
keyword in python is primarily used to delete objects in Python. Since everything in python represents an object in one way or another, The del
keyword can also be used to delete a list, slice a list, delete a dictionaries, remove key-value pairs from a dictionary, delete variables, etc.
Syntax: del object_name
Below are various examples that show-case various use-cases of the del
keyword:
1. del keyword for deleting objects
Example:
In the program below we will deleted Sample_class using del Sample_class
statement.
class Sample_class: some_variable = 20 # method of the class def my_method( self ): print ( "GeeksForGeeks" ) # check if class exists print (Sample_class) # delete the class using del keyword del Sample_class # check if class exists print (Sample_class) |
Output:
class '__main__.Sample_class'
NameError:name 'Sample_class' is not defined
1. del keyword for deleting variables
Example:
In the program below we will delete a variable using del
keyword.
my_variable1 = 20 my_variable2 = "GeeksForGeeks" # check if my_variable1 and my_variable2 exists print (my_variable1) print (my_variable2) # delete both the variables del my_variable1 del my_variable2 # check if my_variable1 and my_variable2 exists print (my_variable1) print (my_variable2) |
Output:
20 GeeksForGeeks 20
NameError: name 'my_variable2' is not defined
1. del keyword for deleting list and list slicing
Example:
In the program below we will delete a list and slice another list using del
keyword.
my_list1 = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] my_list2 = [ "Geeks" , "For" , "Geek" ] # check if my_list1 and my_list2 exists print (my_list1) print (my_list2) # delete second element of my_list1 del my_list1[ 1 ] # check if the second element in my_list1 is deleted print (my_list1) # slice my_list1 from index 3 to 5 del my_list1[ 3 : 5 ] # check if the elements from index 3 to 5 in my_list1 is deleted print (my_list1) # delete my_list2 del my_list2 # check if my_list2 exists print (my_list2) |
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9] ['Geeks', 'For', 'Geek'] [1, 3, 4, 5, 6, 7, 8, 9] [1, 3, 4, 7, 8, 9]
NameError: name 'my_list2' is not defined
1. del keyword for deleting dictionaries and removing key-value pairs
Example:
In the program below we will delete a dictionary and remove few key-value pairs using del
keyword.
my_dict1 = { "small" : "big" , "black" : "white" , "up" : "down" } my_dict2 = { "dark" : "light" , "fat" : "thin" , "sky" : "land" } # check if my_dict1 and my_dict2 exists print (my_dict1) print (my_dict2) # delete key-value pair with key "black" from my_dict1 del my_dict1[ "black" ] # check if the key-value pair with key "black" from my_dict1 is deleted print (my_dict1) # delete my_dict2 del my_dict2 # check if my_dict2 exists print (my_dict2) |
Output:
{'small': 'big', 'black': 'white', 'up': 'down'} {'dark': 'light', 'fat': 'thin', 'sky': 'land'} {'small': 'big', 'up': 'down'}
NameError: name 'my_dict2' is not defined
Please refer delattr() and del() for more details.
Please Login to comment...