Python/gloss python remove list items

来自菜鸟教程
机器人讨论 | 贡献2020年10月29日 (四) 08:48的版本 (机器人:添加分类Python基础教程
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至:导航、​搜索

<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)