Friday, November 15, 2024
Google search engine
HomeLanguagesPython del to delete objects

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 dictionaries, remove key-value pairs from a dictionary, delete variables, etc.

Syntax: del object_name

Below are various examples that showcase various use cases of the del keyword:

Del Keyword for Deleting Objects

In the program below we will delete Sample_class using del Sample_class statement.

Python3




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

Del Keyword for Deleting Variables

In the program below we will delete a variable using del keyword.

Python3




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
NameError: name 'my_variable1' is not defined

Del Keyword for Deleting List and List Slicing

In the program below we will delete a list and slice another list using del keyword.

Python3




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

Del Keyword for Deleting Dictionaries and Removing key-value Pairs

In the program below we will delete a dictionary and remove few key-value pairs using del keyword.

Python3




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.

RELATED ARTICLES

Most Popular

Recent Comments