Open In App

Difference Between Del, Remove and Pop in Python Lists

Last Updated : 13 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

del is a keyword and remove(), and pop() are in-built methods in Python. The purpose of these three is the same but the behavior is different. remove() method deletes values or objects from the list using value and del and pop() deletes values or objects from the list using an index.

del Statement

del is a Python Keyword that is used to delete items from a list by index or to remove the entire list. When using del, we can either specify a single element by index or use slicing to delete a range of elements.

Example:

Python
#Remove elements by index
a = [1, 2, 3, 2, 3, 4, 5]

# use del
del a[3]
print(a)

#Delete a slice of the list
a = [1, 2, 3, 2, 3, 4, 5]

#use del
del a[1:3]
print(a)

Output
[1, 2, 3, 3, 4, 5]
[1, 2, 3, 4, 5]

remove() Method

The remove() method removes the first matching value from the list. It requires the value we want to remove from list as its argument.

Example:

Python
a = [1, 2, 3, 2, 3, 4, 5]

# use remove()
a.remove(3)

# display list
print(a)

Output
[1, 2, 2, 3, 4, 5]

pop() Method

pop() method removes and returns an element from the list. By default, it removes the last element, but we can specify an index to remove an element at a particular position.

Example:

Python
a = [1, 2, 3, 2, 3, 4, 5]

# use remove()
a.pop(3)

# display list
print(a)

Output
[1, 2, 3, 3, 4, 5]

Comparison of del(), remove() and pop()

                 del                

           remove()          

                 pop()             

del is a keyword.It is a method.pop() is a method.
To delete value it uses the index.To delete value this method uses the value as a parameter.This method also uses the index as a parameter to delete.
The del keyword doesn’t return any value.The remove() method doesn’t return any value.pop() returns deleted value.
The del keyword can delete the single value from a list or delete the whole list at a time.At a time it deletes only one value from the list.At a time it deletes only one value from the list.
It throws index error in case of the index doesn’t exist in the list.It throws value error in case of value doesn’t exist in the list.It throws index error in case of an index doesn’t exist in the list.


Next Article

Similar Reads