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

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

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

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

  • 使用remove()方法

  • 使用del关键字

  • 使用pop()方法

  • 使用clear()方法

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

方法1:使用remove()方法

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

语法

list.remove(element)
Return Value:The remove() function will not return any value(returns 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 语句不是一个列表函数。可以使用 del 语句通过传递要删除的项目(元素)的索引来从列表中删除项目

算法(步骤)

以下是执行所需任务的算法/步骤:

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

  • 使用 del 关键字,从列表中删除指定索引(这里是第2个索引(Codes))处的项目。

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

示例

下面的程序使用 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']

方法三:使用pop()方法

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

索引的起始值为0(基于零的索引)。

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

可以使用负值表示从末尾计算的位置。最后一个索引为-1。

算法(步骤)

下面是执行所需任务的算法/步骤:

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

  • 使用pop()方法,将列表项的索引作为参数传递给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']

方法四:使用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 关键字。我们还了解了如果列表中不存在该元素,这些方法会生成的错误。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程