Difference Between Del, Remove, Clear and Pop on Python Lists

Posted in /  

Difference Between Del, Remove, Clear and Pop on Python Lists

Vinay Khatri
Last updated on December 11, 2022

    In this Python tutorial, we will learn the three different techniques to remove or delete elements from the list. These are del, remove, and pop. Moreover, we will also know the difference between del, remove, and pop on Python lists. In Python, we don't have arrays. Thus, we use the list data structure as an alternative to arrays to store elements or items in sequential order. Inserting and deleting elements from a list is easy, and there are many methods provided by the list itself for the insertion and removal of elements between the list.

    Del, Remove, Clear, and Pop in Python Lists

    Python del Keyword with a List

    The del is a Python reserve word that is used to delete the complete object or variable from the program. We can use the del keyword to delete the individual list element or the complete list itself. Syntax

    del list_name[index]   # delete specific element
    del list_name          #delete the complete list

    Example

    >>> my_list = [1,2,3,4,5,6,7]
    >>> del my_list[0] #delete 1 from the list
    >>> my_list
    [2, 3, 4, 5, 6, 7]
    
    >>> del my_list         #delete the complete list
    >>> my_list
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'my_list' is not defined

    Python List remove() Method

    remove() is a Python list method that is used to remove a specific value from the list. The remove method accepts the element value as a parameter and deletes the first occurrence of the value from the list. The remove method returns a None value. If the specified value does not exist in the list, then the remove method throws a ValueError . Syntax

    list_name.remove(value)

    Example

    >>> my_list = [1,1,2,2,3,3]
    >>> my_list.remove(2)        #remove the first 2 from the list
    >>> my_list
    [1, 1, 2, 3, 3]
    
    >>> my_list.remove(12)        #try to remove non exist element
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: list.remove(x): x not in list

    Python List pop Method

    By default, the pop() method is used to remove the last element from the list, but it also accepts an optional parameter that represents the index number that is supposed to pop out or delete from the list. The pop() method also returns the value that it popped out or deleted from the list. Syntax

    list_name.pop(index)

    Example

    >>> my_list = [1,1,2,2,3,3]
    
    >>> my_list.pop()
    3
    
    >>> my_list
    [1, 1, 2, 2, 3]
    
    >>> my_list.pop(0) #remove the first element
    1
    
    >>> my_list
    [1, 2, 2, 3]

    Python List clear() Method

    If you want to delete all the elements from the list, then instead of using the pop() method in for loop, we can use the list clear() method that will delete all the elements from the list. Syntax

    list_name.clear()

    Example

    >>> my_list = [1,1,2,2,3,3]
    >>> my_list.clear()
    >>> my_list
    []

    Del, remove, clear, and pop on Python Lists: A Comparison Table

    Parameter del remove() pop() clear()
    Type It is a Python keyword. remove() is a list method. It is a list method. clear() is a list method.
    Deletes del can delete specific elements from the list or the complete list. It can delete a specific value from the list. pop() can delete specific values from the list using its index. The clear() method can delete all the elements from the list, making the list empty.
    Returns It returns None. The remove() method returns None. It returns the deleted value. It returns None.

    Conclusion

    The del keyword can delete an element or an entire list, while the remove method deletes only a specific value from the list. The pop() method is generally used to remove the last element from the list, and the clear() method is used to delete all the elements from the list in one go. Unlike del, remove(), pop(), and clear() are list methods .

    People are also reading:

    Leave a Comment on this Post

    0 Comments