How does del operator work on list in Python?


Lists are one of the four most commonly used data structures provided by Python. A list is a data structure in python that is mutable and has an ordered sequence of elements. Following is a list of integer values.

lis= [1,2,3,4,5]
print(lis)

If you execute the above snippet, it produces the following output.

[1, 2, 3, 4, 5]

In this article, we will learn how a del operator works on a list in python. They are various scenarios where we use the del operator.

The del operator

The del keyword is mostly used in Python to delete objects. Because everything in Python is an object, the del keyword can be used to delete a list, slice a list, delete dictionaries, remove key-value pairs from a dictionary, delete variables, and so on.

Syntax

The syntax of del operator is as follows.

del object_name

Below are various examples where we use the del operator.

Deleting user-defined objects

In the below example we initially defined a class named MyClass and then when we print it. After that, we delete the class MyClass using the del operator. When we again print the class name it results in an error “NameError: name 'MyClass' is not defined” which means that the defined class is deleted.

Example

class MyClass:
   var = 10
   def func(self):
      print('Hello')
print(MyClass)

del MyClass
print(MyClass)

Output

The above code produces the following results.

<class '__main__.myclass'="">
Traceback (most recent call last):
  File "/home/cg/root/24961/main.py", line 8, in 
    print(MyClass)
NameError: name 'MyClass' is not defined

Deleting variables

Here we have used the del operator to delete variables. Initially, we have declared two variables and printed them. Later on, we deleted these variables and printed these variables which resulted in an error that is observed in the output.

Example

In this example, we will see the working of del operator.

variable1 = 100
variable2 = "Tutorialspoint"
print(variable1)
print(variable2)
del(variable1)
del(variable2)
print(variable1)
print(variable2)

Output

The above code produces the following results.

100
Tutorialspoint
Traceback (most recent call last):
  File "/home/cg/root/26232/main.py", line 7, in 
    print(variable1)
NameError: name 'variable1' is not defined

Deleting an element in a list

The del operator removes an item or an element from the list at the provided index location, but the item is not returned. In short, this operator accepts as an input the item's index to be removed and deletes the item at that index.

Example

In the following example, we deleted an element from a list using the position.

lst = ["Tutorialspoint", "is", "the", "best", "platform", "to", "learn", "new", "skills"]
del lst[4]
print(lst)

Output

The above code produces the following results.

['Tutorialspoint', 'is', 'the', 'best', 'to', 'learn', 'new', 'skills']

Slicing a list

The operator also supports removing a range of items in the list.

Example

In this example, we used the del operator to slice a list.

lst = ["Tutorialspoint", "is", "the", "best", "platform", "to", "learn", "new", "skills"]
del lst[2:5]
print(lst)

Output

The above code produces the following results

['Tutorialspoint', 'is', 'to', 'learn', 'new', 'skills']

Updated on: 04-Apr-2023

404 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements