首先,remove 是删除首个符合条件的元素。并不是删除特定的索引。如下例:
>>> a = [0, 2, 2, 3]
>>> a.remove(2)
>>> a
[0, 2, 3]
而对于 del 来说,它是根据索引(元素所在位置)来删除的,如下例:
>>> a = [3, 2, 2, 1]
>>> del a[1]
[3, 2, 1]
第1个元素为a[0] --是以0开始计数的。则a[1]是指第2个元素,即里面的值2.
del还可以删除整个列表
pop返回的是你弹出的那个数值。 所以使用时要根据你的具体需求选用合适的方法del a
>>> 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 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