del function in Python | Code

  • by

Python has a del statement (keyword) to delete objects, not a del function. The del keyword is used to delete a list, 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 and suggestions on this Python keyword topic.

Note: IDE: PyCharm 2021.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.