Skip to content

del function in Python | Code

Python has a del statement (keyword) to delete objects, not a del function. The del keyword is used to delete a list, or dictionaries, remove key-value pairs from a dictionary, delete variables, etc.

del object_name

Let’s see del examples

Delete Variables, the item from the list, and key-value from a dictionary.

# Delete Variables

my_var = 5
my_tuple = ('John', 25)
my_dict = {'name': 'John', 'age': 25}
del my_var
del my_tuple
del my_dict

# Delete item from list

my_list = [1, 2, 3, 4, 5]
# deleting the third item
del my_list[2]

# Delete from dictionary

person = {'name': 'John',
          'age': 30,
          'profession': 'Developer'
del person['profession']



Do comment if you have any doubts or suggestions on this Python 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

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading