Del keyword in python with examples

In this tutorial, you will understand about del keyword in python, purpose of del keyword with examples step by step.

As name implies Del keyword, It is used to Delete Something, but what? Don’t Worry, I will explain you everything. As we all know that, python is an object orient programming an everything in python is object.

What is del Keyword?

The del keyword is basically used to delete object, variables, list, dictionaries and tuples completely. The main purpose of del keyword is deleting unused object completely and help you to minimize the size of your program

Syntax: Python del Keyword

del object_name

Deleting object in Python

Let understand, how del keyword is used to delete object.

To delete object in python, you have to write del keyword with object name like del object_name.

Example:

class Toppers:
    name="Roy"
    id=12
del Toppers

print(Toppers)
Deleting-Class-in-Python

Deleting Variable in Python

You can also delete variable with the help of del keyword.

Syntax: Deleting variable in python

del variable_name

Example: Deleting variable in python

x="Good Morning"
del x
print(x)
Deleting-Variable-in-python

Deleting list item or list

With the help of del keyword, you can also able to delete list item of specific position.

Syntax: Deleting list item

del list_name[index_position]

Example: Deleting list item

l=[1,2,3,4]

# Deleting variable of index 1
del l[1]
# output:1,3,4
print(l)
Deleting-list-item-in-python

You can also delete list completely in python with the help of del keyword.

Syntax: Deleting list

del l

Example: Deleting list

l=[1,2,3]
del l
print(l)

Deleting Dictionary in python

With the help del keyword, you can also delete item of dictionary or complete dictionary. For deleting particular item in dictionary, you have to used key of that dictionary because dictionary is unorder. You cannot used index value for dictionary.

Syntax: Deleting list item

del dictionary_name['key']

Example: Deleting list item

d={'one':1,'two':2,'three':3}

del d['one']
# output:{'two':2,'three:3'}
print(d)
Deleting-dictionary-item-in-python

You can also delete dictionary completely in python with the help of del keyword.

Syntax: Deleting Dictionary

del dictionary_name

Example: Deleting Dictionary

d={'one':1,'two':2,'three':3}
del d
print(d)

Deleting tuple in python

You can also delete tuple, here we are talking about deleting complete tuple not it’s item because you cannot delete tuple item in python. Tuple is non-modifiable.

Syntax: Deleting Tuple

del tuple_name

Example: Deleting Tuple

t=(1,2,3)
del t
print(t)
Deleting tuple in python

Question answer related to del keywords in python.

How do I remove a value from a list in Python?

Deleting-list-item-in-python

With the help of del keyword, you can are able to remove value from list of item at specific position.
Example:
l=[1,2,3,4]
#Deleting variable of index 1
del l[1]
print(l)
output:
[1,3,4]

How do you use del in python?

Deleting-Class-in-Python

For using del in python, you have use del keyword itself with deleting object.
Example:
class Toppers:
name=”Roy”
id=12
del Toppers
print(Toppers)

Was this article helpful?
YesNo

1 thought on “Del keyword in python with examples”

Leave a Comment