Python 删除列表项
删除列表项
有几种方法可以从列表中删除项目
示例
remove()
方法删除指定的项目
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
自己动手试一试 »
示例
pop()
方法删除指定的索引(如果未指定索引,则删除最后一个项目)
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
自己动手试一试 »
示例
del
关键字删除指定的索引
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)
自己动手试一试 »
示例
del
关键字也可以完全删除列表
thislist = ["apple", "banana", "cherry"]
del thislist
自己动手试一试 »
示例
clear()
方法清空列表
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)
自己动手试一试 »
W3schools 学习路径
跟踪您的进度 - 免费!