Home > Article > Backend Development > How to delete elements from a list in python
The following are examples of several python methods to delete elements in a list
Method 1
Use remove( "") method deletes the specified element. If there is no such element, an error will be reported.
>>> number=[1,3,2,0] >>> number.remove(1)#删除指定元素1,这里是int类型因此不需要引号 >>> print(number) [3, 2, 0]
Method 2
>>> number=[1,3,2,0] >>> del number[3]#删除指定索引数的元素,这里0的索引数是3 >>> print(number) [1, 3, 2]
Method 3
>>> number=[1,3,2,0] >>> number.pop(1)#弹出索引数为1的元素 3 >>> print(number)#由此看出pop方法可以改变原列表 [1, 2, 0]
Method 4
>>> number=[1,3,2,0] >>> number.clear() >>> number []
The above is the detailed content of How to delete elements from a list in python. For more information, please follow other related articles on the PHP Chinese website!