Python列表remove()方法

Python列表remove()方法用于从列表中删除给定的对象。

语法

以下是remove()方法的语法 -

list.pop(obj = list[-1])

参数

  • obj - 这是一个可选参数,要从列表中删除的对象的索引。

返回值

  • 此方法不返回任何值,但从列表中删除给定的对象。

示例

以下示例显示了remove()方法的用法。

#!/usr/bin/python3

list1 = ['physics', 'Biology', 'chemistry', 'maths']
list1.remove('Biology')
print ("list now : ", list1)
list1.remove('maths')
print ("list now : ", list1)

当运行上面的程序,它产生以下结果 -

list now :  ['physics', 'chemistry', 'maths']
list now :  ['physics', 'chemistry']

上一篇: Python列表 下一篇: Python元组