Table of Contents
In this tutorial, you will learn to use the del keyword with the help of examples.
The Python del keyword is used to delete objects. Its syntax is:
# delete obj_name del obj_name
Here, obj_name can be variables, user-defined objects, lists, items within lists, dictionaries etc.
1. Example 1: Delete an user-defined object
class MyClass:
a = 10
def func(self):
print('Hello')
# Output:
print(MyClass)
# deleting MyClass
del MyClass
# Error: MyClass is not defined
print(MyClass)
In the program, we have deleted MyClass using del MyClass statement.
2. Example 2: Delete variable, list, and dictionary
my_var = 5
my_tuple = ('Sam', 25)
my_dict = {'name': 'Sam', 'age': 25}
del my_var
del my_tuple
del my_dict
# Error: my_var is not defined
print(my_var)
# Error: my_tuple is not defined
print(my_tuple)
# Error: my_dict is not defined
print(my_dict)
3. Example 3: Remove items, slices from a list
The del statement can be used to delete an item at a given index. It can also be used to remove slices from a list.
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # deleting the third item del my_list[2] # Output: [1, 2, 4, 5, 6, 7, 8, 9] print(my_list) # deleting items from 2nd to 4th del my_list[1:4] # Output: [1, 6, 7, 8, 9] print(my_list) # deleting all elements del my_list[:] # Output: [] print(my_list)
4. Example 4: Remove a key:value pair from a dictionary
person = { 'name': 'Sam',
'age': 25,
'profession': 'Programmer'
}
del person['profession']
# Output: {'name': 'Sam', 'age': 25}
print(person)
5. del With Tuples and Strings
Note: You can’t delete items of tuples and strings in Python. It’s because tuples and strings are immutables; objects that can’t be changed after their creation.
my_tuple = (1, 2, 3) # Error: 'tuple' object doesn't support item deletion del my_tuple[1]
However, you can delete an entire tuple or string.
my_tuple = (1, 2, 3) # deleting tuple del my_tuple
Related posts:
Python int()
Python List count()
Python List clear()
Python Shallow Copy and Deep Copy
Python String isspace()
Python id()
Python Program to Transpose a Matrix
Python Dictionary update()
Python Program to Check if a Number is Odd or Even
Python Program to Check if a Number is Positive, Negative or 0
Python String rindex()
Python String isupper()
Python Program to Find Armstrong Number in an Interval
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Dictionary copy()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python String title()
Python Object Oriented Programming
Python staticmethod()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Program to Print Output Without a Newline
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python List insert()
Python Program to Delete an Element From a Dictionary
Python Program to Represent enum
Python Errors and Built-in Exceptions
Python Program to Differentiate Between type() and isinstance()
Python list()
Python sleep()
Python Program to Calculate the Area of a Triangle
Python Dictionary clear()