Python/gloss python remove list items

来自菜鸟教程
跳转至:导航、​搜索

<languages />

Python删除列表项

删除列表项

有几种方法可以从列表中删除项目:

The remove() 方法删除指定的项目:

thislist = ["apple", "banana", "cherry"]

thislist.remove("banana")

print(thislist)

The pop() 方法将删除指定的索引(如果未指定index,则删除最后一项):

thislist = ["apple", "banana", "cherry"]

thislist.pop()

print(thislist)

The del 关键字删除指定的索引:

thislist = ["apple", "banana", "cherry"]

  del
thislist[0]
print(thislist)

The del 关键字也可以完全删除列表:

thislist = ["apple", "banana", "cherry"]

  del
thislist

The clear() 方法清空列表:

thislist = ["apple", "banana", "cherry"]

  thislist.clear()
print(thislist)