Python del to delete objects - GeeksforGeeks

29 Apr, 2020
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 existsprint(Sample_class)  # delete the class using del keyworddel Sample_class  # check if class existsprint(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 = 20my_variable2 = "GeeksForGeeks"  # check if my_variable1 and my_variable2 existsprint(my_variable1)print(my_variable2)  # delete both the variablesdel my_variable1del my_variable2  # check if my_variable1 and my_variable2 existsprint(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 existsprint(my_list1)print(my_list2)  # delete second element of my_list1del my_list1[1]  # check if the second element in my_list1 is deletedprint(my_list1)  # slice my_list1 from index 3 to 5del my_list1[3:5]  # check if the elements from index 3 to 5 in my_list1 is deletedprint(my_list1)  # delete my_list2del my_list2  # check if my_list2 existsprint(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 existsprint(my_dict1)print(my_dict2)  # delete key-value pair with key "black" from my_dict1del my_dict1["black"]  # check if the  key-value pair with key "black" from my_dict1 is deletedprint(my_dict1)  # delete my_dict2del my_dict2  # check if my_dict2 existsprint(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.
python-basics
Python-Data Type
Python-datatype
Python
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Convert integer to string in Python
Convert string to integer in Python
How to set input type date in dd-mm-yyyy format using HTML ?
Python infinity
Matplotlib.pyplot.title() in Python