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

Posted in /  

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

Vinay Khatri
Last updated on December 25, 2024

    This tutorial will walk you through the difference between del, remove, clear, and pop on Python lists with appropriate examples.

    In Python, we have different techniques, such as del , remove , clear , and pop , to remove or delete elements from the list. Though the purpose of all three techniques is the same, they behave differently. While del is a keyword, remove() , clear() , and pop() are built-in functions.

    The remove() method uses the value to delete values or objects from a Python list . Conversely, del and pop() leverage the index number to delete values or objects. The clear() method removes all elements from the list at once.

    Let us discuss each method and the del keyword in detail below with examples.

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

    1. Python del Keyword

    The del is a reserved word in Python to delete objects. As everything in Python is an object, the del keyword deletes variables, lists, or items of a list. In other words, we can use this keyword to delete an 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

    2. Python List remove() Method

    remove() is a Python list method to remove a specific item or element from the list. If the list contains the same element more than one time, the remove() method removes the first matching element.

    The remove method accepts the element value as a parameter and deletes the first occurrence of the value from the list. It returns a None value. If the specified value does not exist in the list, it 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-existing element
    
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: list.remove(x): x not in list

    3. 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. Simply pass the index number of the element you want to delete to the pop() method. It 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]
    

    4. Python List clear() Method

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

    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

    This was all about the difference between del, remove(), clear(), and pop() in Python. All are used to remove or delete elements from the list but in a different manner. The del keyword and pop() method uses the index number to remove a specific element from the list.

    On the other hand, remove() accepts the element value itself and removes it from the list. 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.

    We hope this article has helped you understand the difference between del, remove(), clear(), and pop() in Python.

    People are also reading:

    Leave a Comment on this Post

    0 Comments