Facebook Page Twitter Page LinkedIn Page
× Python Tutorial Python Advanced Python Resources Python Libraries


The Python del keyword is used to delete objects. As everything in Python is an object, hence, del keyword can also be used to delete variables, sequence, iterable and it's elements.

Syntax

del object

Example: delete an object

In the below example, del keyword is used to delete a given dictionary object.

Info = {
   "name": "John",
   "age": 25,
   "city": "London"
}

del Info
print(Info)

The output of the above code will be:

NameError: name 'Info' is not defined

Example: delete part of sequence and variables

In the below example, del keyword is used to delete a part of a given list object. The del keyword can also be used to delete a variable.

MyList = [1, 2, 3, 4, 5, 6]
del MyList[1:4]
print(MyList)

MyString = 'John'
del MyString
print(MyString)

The output of the above code will be:

[1, 5, 6]

NameError: name 'MyString' is not defined

❮ Python Keywords