Difference between del, remove and pop on lists

前端 未结 12 2118
北海茫月
北海茫月 2020-11-22 04:20
>>> a=[1,2,3]
>>> a.remove(2)
>>> a
[1, 3]
>>> a=[1,2,3]
>>> del a[1]
>>> a
[1, 3]
>>> a= [1,2,3]
>         


        
相关标签:
12条回答
  • 2020-11-22 04:46

    While pop and delete both take indices to remove an element as stated in above comments. A key difference is the time complexity for them. The time complexity for pop() with no index is O(1) but is not the same case for deletion of last element.

    If your use case is always to delete the last element, it's always preferable to use pop() over delete(). For more explanation on time complexities, you can refer to https://www.ics.uci.edu/~pattis/ICS-33/lectures/complexitypython.txt

    0 讨论(0)
  • 2020-11-22 04:49

    Here is a detailed answer.

    del can be used for any class object whereas pop and remove and bounded to specific classes.

    For del

    Here are some examples

    >>> a = 5
    >>> b = "this is string"
    >>> c = 1.432
    >>> d = myClass()
    
    >>> del c
    >>> del a, b, d   # we can use comma separated objects
    

    We can override __del__ method in user-created classes.

    Specific uses with list

    >>> a = [1, 4, 2, 4, 12, 3, 0]
    >>> del a[4]
    >>> a
    [1, 4, 2, 4, 3, 0]
    
    >>> del a[1: 3]   # we can also use slicing for deleting range of indices
    >>> a
    [1, 4, 3, 0]
    

    For pop

    pop takes the index as a parameter and removes the element at that index

    Unlike del, pop when called on list object returns the value at that index

    >>> a = [1, 5, 3, 4, 7, 8]
    >>> a.pop(3)  # Will return the value at index 3
    4
    >>> a
    [1, 5, 3, 7, 8]
    

    For remove

    remove takes the parameter value and remove that value from the list.

    If multiple values are present will remove the first occurrence

    Note: Will throw ValueError if that value is not present

    >>> a = [1, 5, 3, 4, 2, 7, 5]
    >>> a.remove(5)  # removes first occurence of 5
    >>> a
    [1, 3, 4, 2, 7, 5]
    >>> a.remove(5)
    >>> a
    [1, 3, 4, 2, 7]
    

    Hope this answer is helpful.

    0 讨论(0)
  • 2020-11-22 04:49

    The remove operation on a list is given a value to remove. It searches the list to find an item with that value and deletes the first matching item it finds. It is an error if there is no matching item, raises a ValueError.

    >>> x = [1, 0, 0, 0, 3, 4, 5]
    >>> x.remove(4)
    >>> x
    [1, 0, 0, 0, 3, 5]
    >>> del x[7]
    Traceback (most recent call last):
      File "<pyshell#1>", line 1, in <module>
        del x[7]
    IndexError: list assignment index out of range
    

    The del statement can be used to delete an entire list. If you have a specific list item as your argument to del (e.g. listname[7] to specifically reference the 8th item in the list), it'll just delete that item. It is even possible to delete a "slice" from a list. It is an error if there index out of range, raises a IndexError.

    >>> x = [1, 2, 3, 4]
    >>> del x[3]
    >>> x
    [1, 2, 3]
    >>> del x[4]
    Traceback (most recent call last):
      File "<pyshell#1>", line 1, in <module>
        del x[4]
    IndexError: list assignment index out of range
    

    The usual use of pop is to delete the last item from a list as you use the list as a stack. Unlike del, pop returns the value that it popped off the list. You can optionally give an index value to pop and pop from other than the end of the list (e.g listname.pop(0) will delete the first item from the list and return that first item as its result). You can use this to make the list behave like a queue, but there are library routines available that can provide queue operations with better performance than pop(0) does. It is an error if there index out of range, raises a IndexError.

    >>> x = [1, 2, 3] 
    >>> x.pop(2) 
    3 
    >>> x 
    [1, 2]
    >>> x.pop(4)
    Traceback (most recent call last):
      File "<pyshell#1>", line 1, in <module>
        x.pop(4)
    IndexError: pop index out of range
    

    See collections.deque for more details.

    0 讨论(0)
  • 2020-11-22 04:50

    Remove basically works on the value . Delete and pop work on the index

    Remove basically removes the first matching value. Delete deletes the item from a specific index Pop basically takes an index and returns the value at that index. Next time you print the list the value doesnt appear.

    0 讨论(0)
  • 2020-11-22 04:51

    Use del to remove an element by index, pop() to remove it by index if you need the returned value, and remove() to delete an element by value. The last requires searching the list, and raises ValueError if no such value occurs in the list.

    When deleting index i from a list of n elements, the computational complexities of these methods are

    del     O(n - i)
    pop     O(n - i)
    remove  O(n)
    
    0 讨论(0)
  • 2020-11-22 04:52

    The effects of the three different methods to remove an element from a list:

    remove removes the first matching value, not a specific index:

    >>> a = [0, 2, 3, 2]
    >>> a.remove(2)
    >>> a
    [0, 3, 2]
    

    del removes the item at a specific index:

    >>> a = [9, 8, 7, 6]
    >>> del a[1]
    >>> a
    [9, 7, 6]
    

    and pop removes the item at a specific index and returns it.

    >>> a = [4, 3, 5]
    >>> a.pop(1)
    3
    >>> a
    [4, 5]
    

    Their error modes are different too:

    >>> a = [4, 5, 6]
    >>> a.remove(7)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: list.remove(x): x not in list
    >>> del a[7]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: list assignment index out of range
    >>> a.pop(7)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: pop index out of range
    
    0 讨论(0)
提交回复
热议问题