What is difference between del, remove, and pop on lists in Python

Welcome to akashITS (Akash IT Solutions), In this article, you will learn about: What is difference between del, remove, and pop on lists in Python

What is difference between del, remove, and pop on lists in Python

In this article, we will learn about the differences between delete, remove and pop methods in python.

All these methods are built-in methods available in python. These methods are used for either deleting or removing items from a list.

‘del’ Statement:

The del statement is a general-purpose way to remove elements from a list or even delete the entire list itself. It operates based on the index of the element to be removed.

Syntax

del List_name(index)

Example: del

# Removing an element by index
my_list = [1, 2, 3, 4, 5,6]
del my_list[2]
# Removes the element at index 2
print(my_list)
# Output: [1, 2, 4, 5, 6]

# Deleting the entire list
del my_list
# Now, my_list is not defined, and attempting to use it will result in an error

Using del allows you to delete elements by index, slices, or the entire list. However, it doesn’t return the deleted elements and can raise an IndexError if the specified index is out of range.

‘remove’ Method:

The remove method is specifically designed to remove the first occurrence of a specified value from the list. It does not operate based on the index but on the element itself.

Syntax:

List_name.remove(element)

Examples: remove()

# Removing an element by value
my_list = [1, 2, 3, 4, 3, 5]
# Removes the first occurrence of the value 3
my_list.remove(3)
print(my_list)
# Output: [1, 2, 4, 3, 5]

Unlike del, remove requires the value of the element to be removed as an argument. If the specified value is not found in the list, a ValueError is raised. It also does not return the removed element.

‘pop’ Method:

The pop method is used to remove and return an element from a specific index in the list. If no index is provided, it removes and returns the last element by default.

Syntax:

List_name.pop(index_no)

Examples: pop()

# Removing an element by index using pop
my_list = [1, 2, 3, 4, 5]
removed_element = my_list.pop(2)  # Removes and returns the element at index 2
print(my_list)
# Output: [1, 2, 4, 5]
print(removed_element)
# Output: 3

pop is useful when you need to access and use the removed element after deletion. If the index is not specified or is out of range, it raises an IndexError. If the list is empty, a IndexError is also raised.

Key Differences:

  • Targeted Removal:
    • del: It removes elements based on index or slices.
    • remove: It removes elements based on value.
    • pop: It removes and returns elements based on index (or last element if no index is specified).
  • Return Value:
    • del: It does not return the deleted elements.
    • remove: It does not return the deleted element.
    • pop: It returns the deleted element.
  • Handling Errors:
    • del: It may raise IndexError if the specified index is out of range.
    • remove: It gives ValueError if the specified value is not found.
    • pop: It gives IndexError if the specified index is out of range or if the list is empty.

Conclusion

Python pop() vs remove() vs del function 

  • The remove() function removes the first matching value from the list.
  • The pop() function is used to return the removed element from the list.
  • The del() function is used to delete an element at a specified index number in the list.

Leave a Comment