删除列表中的元素
1.删除指定下标的元素
>>> a = [1,2,3,4,5]>>> del a[2] >>> a [1, 2, 4, 5]
del容易犯的错误
>>> a = [1,2,3,4,5] >>> b = del a[2] 不能直接将删除的赋值给另一个变量 File "<stdin>", line 1 b = del a[2] ^ SyntaxError: invalid syntax >>> a = [1,2,3,4,5] >>> del a[2] >>> b = a >>> a [1, 2, 4, 5] >>> b [1, 2, 4, 5]
>>> a = [1,2,3,4,5] >>> a.del(2) 不能用.的方式访问del,并且不能是小括号() File "<stdin>", line 1 a.del(2) ^ SyntaxError: invalid syntax >>> a.del[2] File "<stdin>", line 1 a.del[2] ^ SyntaxError: invalid syntax >>> del a(2) File "<stdin>", line 1 SyntaxError: can't delete function call >>> del a[2] >>> a [1, 2, 4, 5]
2.删除最后一个元素
>>> a = [1,2,3,4,5] >>> a.pop() 5
3.删除list中某一个值
>>> a = [1,2,3,4,5] >>> a.remove(2) >>> a [1, 3, 4, 5]