What is del method in python?

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.


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.


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)

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)

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)

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

The del keyword in python is primarily used to delete objects in Python. Since everything in python represents an object in one way or another, The del keyword can also be used to delete a list, slice a list, delete a dictionaries, remove key-value pairs from a dictionary, delete variables, etc.


Syntax: del object_name

Below are various examples that show-case various use-cases of the del keyword:

1. del keyword for deleting objects

Example:
In the program below we will deleted Sample_class using del Sample_class statement.

class Sample_class:

    some_variable = 20

    def my_method(self):

        print("GeeksForGeeks")

print(Sample_class)

del Sample_class

print(Sample_class)

Output:

class '__main__.Sample_class'
NameError:name 'Sample_class' is not defined

1. del keyword for deleting variables

Example:
In the program below we will delete a variable using del keyword.

my_variable1 = 20

my_variable2 = "GeeksForGeeks"

print(my_variable1)

print(my_variable2)

del my_variable1

del my_variable2

print(my_variable1)

print(my_variable2)

Output:

20
GeeksForGeeks
20
NameError: name 'my_variable2' is not defined

1. del keyword for deleting list and list slicing

Example:
In the program below we will delete a list and slice another list using del keyword.

my_list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]

my_list2 =["Geeks", "For", "Geek"]

print(my_list1)

print(my_list2)

del my_list1[1]

print(my_list1)

del my_list1[3:5]

print(my_list1)

del my_list2

print(my_list2)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
['Geeks', 'For', 'Geek']
[1, 3, 4, 5, 6, 7, 8, 9]
[1, 3, 4, 7, 8, 9]
NameError: name 'my_list2' is not defined

1. del keyword for deleting dictionaries and removing key-value pairs

Example:
In the program below we will delete a dictionary and remove few key-value pairs using del keyword.

my_dict1 = {"small": "big", "black": "white", "up": "down"}

my_dict2 = {"dark": "light", "fat": "thin", "sky": "land"}

print(my_dict1)

print(my_dict2)

del my_dict1["black"]

print(my_dict1)

del my_dict2

print(my_dict2)

Output:

{'small': 'big', 'black': 'white', 'up': 'down'}
{'dark': 'light', 'fat': 'thin', 'sky': 'land'}
{'small': 'big', 'up': 'down'}
NameError: name 'my_dict2' is not defined

Please refer delattr() and del() for more details.


What is Del method?

The del keyword in python is primarily used to delete objects in Python. Since everything in python represents an object in one way or another, The del keyword can also be used to delete a list, slice a list, delete a dictionaries, remove key-value pairs from a dictionary, delete variables, etc. Syntax: del object_name.

What is the use of Del statement?

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.

How does Del work in Python list?

Python del operator: The del operator removes the item or an element at the specified index location from the list, but the removed item is not returned, as it is with the pop() method. So essentially, this operator takes the item's index to be removed as the argument and deletes the item at that index.

How do you use Del?

The preposition de is translated as "of," "from," or "about," but de also can mean "by," "in," or other prepositions in some cases. Del is simply the contraction of de and the definite article el (not él), so we use del in place of de el.