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元組