Skip to content

del keyword in Python | Example code

  • by

Python del keyword is used to delete objects. It could delete everything from an object like variables, lists, dictionaries, slice a list, etc.

del object_name

Examples del keyword in Python

Simple example code to delete variables, lists, dictionaries, and class objects.

Deleting variables

Delete both the variables

num = 10
s = "Hello"

del num
del s



del keyword in Python

Deleting list

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]

del list1



NameError: name ‘list1’ is not defined

Deleting dictionaries

dict1 = {"small": "big", "black": "white", "up": "down"}

del dict1



NameError: name ‘dict1’ is not defined

Deleting objects

class MyClass:
    name = "John"

del MyClass



NameError: name ‘MyClass’ is not defined

Do comment if you have any questions or suggestions on this Python del keyword topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.