>>> a=[1,2,3]
>>> a.remove(2)
>>> a
[1, 3]
>>> a=[1,2,3]
>>> del a[1]
>>> a
[1, 3]
>>> a= [1,2,3]
>>> a.pop(1)
2
>>> a
[1, 3]
>>>
Is there any difference between the above three methods to remove an element from a list?
回答 0
是的,remove删除第一个匹配值,而不是特定的索引:
>>> a =[0,2,3,2]>>> a.remove(2)>>> a[0,3,2]
del 删除特定索引处的项目:
>>> a =[9,8,7,6]>>>del a[1]>>> a[9,7,6]
并pop从特定索引处删除该项目并返回。
>>> a =[4,3,5]>>> a.pop(1)3>>> a[4,5]
它们的错误模式也不同:
>>> a =[4,5,6]>>> a.remove(7)Traceback(most recent call last):File"<stdin>", line 1,in<module>ValueError: list.remove(x): x notin 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
Yes, 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
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 latter 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
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.
回答 4
这里有很多最佳的解释,但我会尽力简化一下。
在所有这些方法中,reverse和pop是后缀,而delete是prefix。
remove():用于删除元素的第一次出现
remove(i) =>第一次出现i值
>>> 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]
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)
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.
>>> 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
>>> 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
>>> 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
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
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.