The del keyword in Python is used to delete objects like variables, lists, dictionary entries, or slices of a list. Since everything in Python is an object, del helps remove references to these objects and can free up memory
del Keyword removes the reference to an object. If that object has no other references, it gets cleared from memory. Trying to access a deleted variable or object will raise a NameError.
Syntax
del object_name
object_name: name of the object to delete (list, set dictionary, etc).
Del Keyword for Deleting Objects
In the example below, we will delete Gfg_class using
del
statement.
Python
class Gfg_class:
a = 20
# creating instance of class
obj = Gfg_class()
# delete object
del obj
# we can also delete class
del Gfg_class
Deleting Variables
del keyword can be used to delete variables too.
Python
a = 20
b = "GeeksForGeeks"
# delete both the variables
del a, b
# check if a and b exists after deleting
print(a)
print(b)
Output:
NameError: name 'a' is not defined
Explanation: del a, b removes the existence of a and b therefore print() funciton is not able to find them.
List Slicing Using del Keyword
In the program below we will delete some parts of a list (basically slice the list) using del keyword.
Python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# delete second element of 'a'
del a[1]
# check if the second element in 'a' is deleted
print(a)
# slice 'a' from index 3 to 5
del a[3:5]
# check if the elements from index 3 to 5 in 'a' is deleted
print(a)
Output[1, 3, 4, 5, 6, 7, 8, 9]
[1, 3, 4, 7, 8, 9]
Deleting Dictionary and Removing key-value Pairs
In the program below we will remove few key-value pairs from a dictionary using del keyword.
Python
d = {"small": "big", "black": "white", "up": "down"}
# delete key-value pair with key "black" from my_dict1
del d["black"]
# check if the key-value pair with key "black" from d1 is deleted
print(d)
Output{'small': 'big', 'up': 'down'}
Please refer delattr() and del() for more details.
Similar Reads
Python def Keyword Python def keyword is used to define a function, it is placed before a function name that is provided by the user to create a user-defined function. In Python, a function is a logical unit of code containing a sequence of statements indented under a name given using the âdefâ keyword. In Python def
6 min read
Python in Keyword The in keyword in Python is a powerful operator used for membership testing and iteration. It helps determine whether an element exists within a given sequence, such as a list, tuple, string, set or dictionary.Example:Pythons = "Geeks for geeks" if "for" in s: print("found") else: print("not found")
3 min read
as Keyword - Python as keyword in Python plays a important role in simplifying code, making it more readable and avoiding potential naming conflicts. It is mainly used to create aliases for modules, exceptions and file operations. This powerful feature reduces verbosity, helps in naming clarity and can be essential whe
3 min read
Python Keywords Keywords in Python are reserved words that have special meanings and serve specific purposes in the language syntax. Python keywords cannot be used as the names of variables, functions, and classes or any other identifier. Getting List of all Python keywordsWe can also get all the keyword names usin
11 min read
python class keyword In Python, class keyword is used to create a class, which acts as a blueprint for creating objects. A class contains attributes and methods that define the characteristics of the objects created from it. This allows us to model real-world entities as objects with their own properties and behaviors.S
1 min read