del in Python

del in Python | In this post, we will discuss how to delete objects using the del operator. In Python everything is an object, so the del keyword can also be used to delete user-defined objects, variables, lists, or parts of a list, etc. The del statement works by unbinding the name, removing it from the set of names known to the Python interpreter. If this variable was the last remaining reference to an object, the object will be removed from memory. If, on the other hand, other variables still refer to this object, the object won’t be deleted.

Syntax:

del obj_name

Here, del is a Python keyword and obj_name can be user-defined objects, variables, dictionaries, lists, items within lists, etc.

del Function in Python

Here, we will develop a python program to delete user-defined objects. First, we will take a user-defined object then, we have to delete My_Project using the del My_Project statement.

# Python program to delete user-defined object

# user-defined object
class My_Project:
    x = 5
    def func(self):
        print('Know Program')

# Output
print(My_Project)

# deleting My_Project
del My_Project

# check if class exists
print(My_Project)

Output:-

Traceback (most recent call last):
File “main.py”, line 16, in
print(My_Project)
NameError: name ‘My_Project’ is not defined

Python delete Variable

# Python program to delete variable

# take variable
var = 25

# deleting variable
del var

# check if class exists
print(var)

Output:-

Traceback (most recent call last):
File “main.py”, line 10, in
print(var)
NameError: name ‘var’ is not defined

del List in Python

# Python program to delete list

# take list
my_list = ['Know Program', 'Python', 8, 'Java', 25,]

# deleting list
del my_list

# check if class exists
print(my_list)

Output:-

NameError: name ‘my_list’ is not defined

Python del Dictionary

# Python program to delete dictionary

# take dictionary
my_dict = {'Know Program', 'Python', 8, 'Java', 25,}

# deleting dictionary
del my_dict

# check if class exists
print(my_dict)

Output:-

NameError: name ‘my_dict’ is not defined

Python del Tuples

# Python program to delete tuples

# take tuples
my_tuple = (5, 10, 15, 20, 25)

# deleting tuple
del my_tuple

# check if class exists
print(my_tuple)

Output:-

NameError: name ‘my_tuple’ is not defined

Python Delete Element from List

The del statement can be used to delete an item or element at a given index.

# Python program to delete element from list by index

# take list
my_list = ['C', 'Java', 'Python', 'HTML', 'Javascript']

# printing original list
print('List:', my_list)

# removed first item from the list
del my_list[0]

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘HTML’, ‘Javascript’]
New list: [‘Java’, ‘Python’, ‘HTML’, ‘Javascript’]

The del operator will delete multiple items from the list using slicing. del[a : b] :- This method deletes all the elements in range starting from index ‘a’ till ‘b’ mentioned in arguments.

# Python program to delete item from list

# take list
my_list = ['C', 'Java', 'Python', 'HTML', 'Javascript']

# printing original list
print('List:', my_list)

# removed item from the list
del my_list[2:4]

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘HTML’, ‘Javascript’]
New list: [‘C’, ‘Java’, ‘Javascript’]

Remove Value from Dictionary in Python

# Python program to delete item from dictionary

# take dictionary
my_dict = { 'name': 'Guddu',  'age': 21,  'profession': 'Programmer'}

# printing original dictionary
print('Dictionary:', my_dict)

# removed item from the dictionary
del my_dict['profession']

# print dictionary after item deletion
print('New Dictionary:', my_dict)

Output:-

Dictionary: {‘profession’: ‘Programmer’, ‘name’: ‘Guddu’, ‘age’: 21}
New Dictionary: {‘name’: ‘Guddu’, ‘age’: 21}

Python delete item from the Tuples

The del statement can’t delete items or elements of tuples and strings. It’s because tuples and strings are immutables; objects that can’t be changed after their creation.

# Python program to delete item from tuples

# take tuples
my_tuple = (5, 10, 15, 20, 25)

# removed item from the tuple
del my_tuple[3]

# print tuple after item deletion
print(my_tuple)

Output:-

Traceback (most recent call last):
File “main.py”, line 7, in
del my_tuple[3]
TypeError: ‘tuple’ object doesn’t support item deletion

Also See:- Python List pop() Function

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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