如何在 Python 中从列表中删除对象?

pythonserver side programmingprogramming

要从 Python 中的列表中删除项目(元素),请使用列表函数 clear()pop()remove()。您还可以使用 del 语句删除项目,方法是使用索引或切片指定位置或范围。

在本文中,我们将向您展示如何使用 Python 从列表中删除对象/元素。以下是完成此任务的 4 种不同方法 -

  • 使用 remove() 方法

  • 使用 del 关键字

  • 使用 pop() 方法

  • 使用 clear() 方法

假设我们获取了一个包含一些元素的列表。使用上述不同方法从输入列表中删除给定项后,我们将返回结果列表。

方法 1:使用 remove() 方法

remove() 函数删除作为参数传递给它的给定项。 −

语法

list.remove(element)
返回值:remove() 函数不会返回任何值(返回 None)

算法(步骤)

以下是执行所需任务需要遵循的算法/步骤

  • 创建一个变量来存储输入列表。

  • 使用 remove() 方法从输入列表中删除特定项目,方法是将要删除的列表项作为参数传递给该方法并将其应用于输入列表。

  • 从输入列表中删除指定项目后打印结果列表

示例

以下程序使用从输入列表中删除指定项目remove() 方法并打印结果列表 −

# input list inputList = ['TutorialsPoint', 'Python', 'Codes', 'hello', 'everyone'] # Removing the 'TutorialsPoint' from the list using the remove() method inputList.remove("TutorialsPoint") # Printing the result list print("Input List after removing {TutorialsPoint}:\n", inputList)

输出

执行时,上述程序将生成以下输出 -

Input List after removing {TutorialsPoint}:
['Python', 'Codes', 'hello', 'everyone']

作为代码的输入,我们获得了一个包含一些随机值的示例列表,以及要从列表中删除的对象/元素。然后将元素传递给 remove() 方法,在该方法中将其从列表中删除/移除。如果在列表中找不到对象/元素,则将返回值错误。

方法 2:使用 del 关键字

del 语句不是 List 函数。 del 语句可用于通过传递要删除的项目(元素)的索引来从列表中删除项目

算法(步骤)

以下是执行所需任务所要遵循的算法/步骤 -

  • 创建一个变量来存储输入列表。

  • 使用 del 关键字从列表中删除指定索引处的项目(此处为第二个索引(代码))。

  • 打印结果列表,即从列表中删除指定项目后。

示例

以下程序使用 del 关键字并打印结果列表 −

# input list inputList = ['TutorialsPoint', 'Python', 'Codes', 'hello', 'everyone'] # Removing the item present at the 2nd index(Codes) from the list del inputList[2] # Printing the result list print("Input List after removing the item present at the 2nd index{Codes}:\n", inputList)

输出

执行时,上述程序将生成以下输出 -

Input List after removing the item present at the 2nd index{Codes}:
['TutorialsPoint', 'Python', 'hello', 'everyone']

方法 3:使用 pop() 方法

使用 pop() 方法,您可以删除指定位置的元素并检索其值。

索引的起始值为 0(从零开始的索引)。

如果没有指定索引,pop() 方法将从列表中删除最后一个元素。

可以使用负值来表示距离末尾的位置。最后一个索引是 -1

算法(步骤)

以下是执行所需任务需要遵循的算法/步骤 -

  • 创建一个变量来存储输入列表。

  • 使用 pop() 方法,绕过列表项的索引作为参数,以删除给定索引处的项目

  • 将负索引值作为参数传递给 pop() 方法,以从最后一个删除列表项。这里,为了从列表中删除最后一个项目,我们将 -1 索引作为参数传递给 pop() 方法。

  • 打印结果列表,即从列表中删除指定项目后。

示例

以下程序使用 pop() 方法从输入列表中删除指定项目并打印结果列表 −

# input list inputList = ['TutorialsPoint', 'Python', 'Codes', 'hello', 'everyone'] # Removing the 'TutorialsPoint' from the list using the pop() method del_item = inputList.pop(0) # Removing the 'everyone' from the list by passing the negative index -1 last_del_item = inputList.pop(-1) # Printing the result list print("Input List after removing {TutorialsPoint}, {everyone}:\n", inputList)

输出

Input List after removing {TutorialsPoint}, {everyone}:
['Python', 'Codes', 'hello']

方法 4:使用 clear() 方法

clear() 方法从列表中删除所有项目。列表仍然存在,但它是空的。

算法(步骤)

以下是执行所需任务需要遵循的算法/步骤 -

  • 创建一个变量来存储输入列表。

  • 使用 clear() 方法从列表中删除所有项目。

  • 打印结果列表,即从列表中删除所有内容之后。

示例

以下程序使用 clear() 函数清除或清空整个列表 -

# input list inputList = ['TutorialsPoint', 'Python', 'Codes', 'hello', 'everyone'] # Removing all the items from the list inputList.clear() # Printing the result list print("Input List after removing all the items:", inputList)

输出

Input List after removing all the items: []

结论

在本文中,我们学习了如何使用四种不同的方法从列表中删除对象/元素:remove()pop()clear()del 关键字。我们还了解了如果元素不在列表中,这些方法会生成的错误。


相关文章