Difference between del, remove and pop on lists

前端 未结 12 2117
北海茫月
北海茫月 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:27

    You can also use remove to remove a value by index as well.

    n = [1, 3, 5]
    
    n.remove(n[1])
    

    n would then refer to [1, 5]

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

    Already answered quite well by others. This one from my end :)

    Evidently, pop is the only one which returns the value, and remove is the only one which searches the object, while del limits itself to a simple deletion.

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

    pop

    Takes index (when given, else take last), removes value at that index, and returns value

    remove

    Takes value, removes first occurrence, and returns nothing

    delete

    Takes index, removes value at that index, and returns nothing

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

    Since no-one else has mentioned it, note that del (unlike pop) allows the removal of a range of indexes because of list slicing:

    >>> lst = [3, 2, 2, 1]
    >>> del lst[1:]
    >>> lst
    [3]
    

    This also allows avoidance of an IndexError if the index is not in the list:

    >>> lst = [3, 2, 2, 1]
    >>> del lst[10:]
    >>> lst
    [3, 2, 2, 1]
    
    0 讨论(0)
  • 2020-11-22 04:44

    Many best explanations are here but I will try my best to simplify more.

    Among all these methods, reverse & pop are postfix while delete is prefix.

    remove(): It used to remove first occurrence of element

    remove(i) => first occurrence of i value

    >>> a = [0, 2, 3, 2, 1, 4, 6, 5, 7]
    >>> a.remove(2)   # where i = 2
    >>> a
    [0, 3, 2, 1, 4, 6, 5, 7]
    

    pop(): It used to remove element if:

    unspecified

    pop() => from end of list

    >>>a.pop()
    >>>a
    [0, 3, 2, 1, 4, 6, 5]
    

    specified

    pop(index) => of index

    >>>a.pop(2)
    >>>a
    [0, 3, 1, 4, 6, 5]
    

    WARNING: Dangerous Method Ahead

    delete(): Its a prefix method.

    Keep an eye on two different syntax for same method: [] and (). It possesses power to:

    1.Delete index

    del a[index] => used to delete index and its associated value just like pop.

    >>>del a[1]
    >>>a
    [0, 1, 4, 6, 5]
    

    2.Delete values in range [index 1:index N]

    del a[0:3] => multiple values in range

    >>>del a[0:3]
    >>>a
    [6, 5]
    

    3.Last but not list, to delete whole list in one shot

    del (a) => as said above.

    >>>del (a)
    >>>a
    

    Hope this clarifies the confusion if any.

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

    Any operation/function on different data structures is defined for particular actions. Here in your case i.e. removing an element, delete, Pop and remove. (If you consider sets, Add another operation - discard) Other confusing case is while adding. Insert/Append. For Demonstration, Let us Implement deque. deque is a hybrid linear data structure, where you can add elements / remove elements from both ends.(Rear and front Ends)

    class Deque(object):
    
      def __init__(self):
    
        self.items=[]
    
      def addFront(self,item):
    
        return self.items.insert(0,item)
      def addRear(self,item):
    
        return self.items.append(item)
      def deleteFront(self):
    
        return self.items.pop(0)
      def deleteRear(self):
        return self.items.pop()
      def returnAll(self):
    
        return self.items[:]
    

    In here, see the operations:

    def deleteFront(self):
    
        return self.items.pop(0)
    def deleteRear(self):
        return self.items.pop()
    

    Operations have to return something. So, pop - With and without an index. If I don't want to return the value: del self.items[0]

    Delete by value not Index:

    • remove :

      list_ez=[1,2,3,4,5,6,7,8]
      for i in list_ez:
          if i%2==0:
              list_ez.remove(i)
      print list_ez
      

    Returns [1,3,5,7]

    let us consider the case of sets.

    set_ez=set_ez=set(range(10))
    
    set_ez.remove(11)
    
    # Gives Key Value Error. 
    ##KeyError: 11
    
    set_ez.discard(11)
    
    # Does Not return any errors.
    
    0 讨论(0)
提交回复
热议问题