Signup/Sign In

How to Delete Element in Python List

In python list, data structure consists of many built-in functions using which we can do several operations on the list elements. In this tutorial, we will learn how to delete the elements present in the list by the remove(), pop(), del method. The remove() method removes the specified element from the list. If the specified element repeats in the list, it removes the first matching element. The pop() method removes the element in the list. It returns the element present in the list. If no parameter is given, the pop() method removes the last element present in the list. We can delete the particular element from the defined list by using the del keyword.

Example: Deleting elements from the list using the remove() method.

The below example shows how to remove elements from the list using the remove() method.

#Intializing list
list_seq_1=[10,20,30,40,50,60]
list_seq_2=[10,20,30,20,50,60]
print("Elements present in the list before removing:",list_seq_1)
# Removing elements from the list list_seq_1
list_seq_1.remove(50)
print("Elements present in list after modifying are:",list_seq_1)
print("Elements present in the list before removing:",list_seq_2)
# Removing elements from the list list_seq_2
list_seq_2.remove(20)
print("Elements present in list after modifying are:",list_seq_2)

In the above example, we define list_seq_1 with different elements and list_seq_2 with repeated elements. In the list_seq_1 we are trying to remove the element 50 from the list. The remove() method searches the specified element in the list and removes the element from the list. In the list_seq_2 we are trying to remove the element 20 from the list. The element 20 is repeated two times in the list_seq_2. The remove() method searches the specified element in the list and removes the first matching element in the list.

Once we run the program we will get the following result.


Elements present in the list before removing: [10, 20, 30, 40, 50, 60]
Elements present in list after modifying are: [10, 20, 30, 40, 60]
Elements present in the list before removing: [10, 20, 30, 20, 50, 60]
Elements present in list after modifying are: [10, 30, 20, 50, 60]

Example: Type Error.

The remove() method takes one parameter. If we give more than one parameter or no parameters are given, we will get Type Error.

#Intializing list
list_seq=[10,20,30,40,50,60]
print("Elements present in the list before removing:",list_seq)
list_seq.remove()
print("Elements present in list after modifying are:",list_seq)

Once we run the program we will get the following result.


Traceback (most recent call last):
File "C:/PycharmProjects/pythonProject2/module_4.py", line 9, in <module>
list_seq.remove()
TypeError: remove() takes exactly one argument (0 given)
Elements present in the list before removing: [10, 20, 30, 40, 50, 60]

Example: Value Error

We will get ValueError, if the remove method failed to search the specified element.

#Intializing list
list_seq_1=[10,20,30,40,50,60]
print("Elements present in the list before removing:",list_seq_1)
list_seq_1.remove(80)
print("Elements present in list after modifying are:",list_seq_1)

In the above example, we are trying to remove the element from the list list_seq_1. But the specified element is not present in that list.

Once we run the program, it shows the below result.


Traceback (most recent call last):
File "C:/PycharmProjects/pythonProject2/module_4.py", line 10, in <module>
list_seq_1.remove(80)
ValueError: list.remove(x): x not in list
Elements present in the list before removing: [10, 20, 30, 40, 50, 60]

Example: Deleting elements from the list using pop() method with and without parameter.

The below example shows how to remove elements from the list using the pop() method.

#Intializing list
list_seq_1=[10,20,30,40,50,60]
print("Elements present in the list before removing are:",list_seq_1)
list_seq_1.pop(2)
print("Elements present in list after removing are:",list_seq_1)
list_seq_1.pop()
print("Elements present in list after removing are:",list_seq_1)

In the above example, we are using the pop() method to remove the element in the list. First, we are trying to remove the element by giving an index value. The pop() method removes the element present in the specified index. Next, we are trying to remove the element. But there is no parameter given, the pop() method removes the last element in the list.

Once we run the program, it shows the below result.


Elements present in the list before removing are: [10, 20, 30, 40, 50, 60]
Elements present in list after removing are: [10, 20, 40, 50, 60]
Elements present in list after removing are: [10, 20, 40, 50]

Example: Index Error

The pop() method raises IndexError when it fails to find the specified index.

#Intializing list
list_seq_1=[10,20,30,40,50,60]
print("Elements present in the list before removing are:",list_seq_1)
list_seq_1.pop(9)
print("Elements present in list after removing are:",list_seq_1)

In the above program, we gave the index number 9 which is out of range.

Once we run the program, it shows the below result.


Traceback (most recent call last):
File "C:/PycharmProjects/pythonProject2/module_4.py", line 9, in <module>
list_seq_1.pop(9)
IndexError: pop index out of range
Elements present in the list before removing are: [10, 20, 30, 40, 50, 60]

Example: Deleting elements from the list using del keyword.

The below example shows how to delete a particular element from the list by using the del keyword.

We can give a negative index also.

#Intializing list
list_seq_1=[10,20,30,40,50,60]
print("Elements present in the list before removing are:",list_seq_1)
del list_seq_1[1]
print("Elements present in list after removing are:",list_seq_1)
del list_seq_1[-1]
print("Elements present in list after removing are:",list_seq_1)

In the above example, we are trying to delete the elements from the list using the del keyword. First, we gave a positive index value. The del keyword removes the element which is present in the index value 1. i.e Element 20. Next, we gave a negative index value. The del keyword removes the element which is present in the index value 1. i.e Element 60.

Once we run the program, it shows the following result.


Elements present in the list before removing are: [10, 20, 30, 40, 50, 60]
Elements present in list after removing are: [10, 30, 40, 50, 60]
Elements present in list after removing are: [10, 30, 40, 50]

Conclusion:

In this tutorial, we learned how to delete elements in the list using the built-in functions of the list. We solved examples and delete the elements present in the list using the remove() method, pop() method, and del keyword.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.