如何在 Python 中根据索引删除列表中的元素?


在本文中,我们将向您展示如何使用 Python 根据索引删除列表中的元素。在这里,我们将看到完成此任务的 4 种方法 -

  • 使用 del 关键字删除列表中的元素

  • 使用 pop() 函数删除列表中的元素

  • 使用切片删除列表中的元素

  • 使用索引删除列表中的多个元素

假设我们已经创建了一个包含一些元素的列表。我们将使用上述方法通过提供索引值从列表中删除特定项。

方法 1:使用 del 关键字删除列表中的元素

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

算法(步骤)

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

  • 输入要删除列表项的索引

  • 使用del关键字删除给定索引处的列表项。

  • 删除指定列表项后打印列表。

语法

"del list object [index]"

这是一个命令,可用于根据其索引位置从列表中删除项目。

如果列表为空或指定的索引超出范围,则 del 关键字将引发 IndexError。

示例

以下程序使用 del 关键字删除指定索引列表元素后返回列表 -

# input list inputList = ["Welcome", "to", "tutorialspoint", "python"] # Enter the index at which the list item is to be deleted givenIndex = 3 # deleting the list item at the given index using the del keyword del inputList[givenIndex] # printing the list after deleting a specified list item print("List after deleting specified list item:", inputList)

输出

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

List after deleting specified list item: ['Welcome', 'to', 'tutorialspoint']

方法 2:使用 pop() 函数删除列表中的元素

算法(步骤)

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

  • 将给定的索引作为参数传递给 pop() 函数(此处 pop() 函数删除给定索引处的列表元素)

  • 删除指定列表项后打印列表。

示例

以下程序使用 pop() 函数删除指定索引列表元素后返回列表 -

inputList = ["Welcome", "to", "tutorialspoint", "python"] givenIndex = 1 # Passing index to the pop function to remove that element of the list at that index inputList.pop(givenIndex) # printing the list after deleting an item at the entered index print("List after deleting an item at the entered index:") print(inputList)

输出

List after deleting an item at the entered index:
['Welcome', 'tutorialspoint', 'python']

方法 3:使用切片删除列表中的元素

切片可用于通过从原始输入列表中删除给定索引处的项目来创建新列表。

将列表分成三部分,以删除索引 N 处的元素。

  • 范围从 0 到 N-1 的项目

  • 索引 N 位置处的项目

  • 从 N+1 开始值到列表末尾的项目。

算法(步骤)

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

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

  • 使用切片存储给定索引之前和之后的列表元素,这意味着它删除了给定索引处的项目。

  • 删除指定列表项后打印列表

示例

inputList = ["Welcome", "to", "tutorialspoint", "python"] givenIndex = 3 # storing the list elements before the given index and after the given index # using slicing which means it removes the item at the given index inputList = inputList[:givenIndex] + inputList[givenIndex+1:] # printing the list after deleting an item at the entered index print("List after deleting an item at the entered index:\n", inputList)

输出

List after deleting an item at the entered index:
['Welcome', 'to', 'tutorialspoint']

方法 4:从列表中删除多个索引元素

算法(步骤)

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

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

  • 输入要删除项目的索引列表

  • 使用sorted()函数(返回给定可迭代对象的排序列表。如果 reverse=True,则按降序排序,如果 reverse=False,则按升序排序。默认为 False,即升序),通过传递给定的索引列表和 reverse=True 作为参数来按降序对给定的索引列表进行排序。

  • 使用 for循环遍历给定的索引列表

  • 使用 if条件语句检查相应的迭代器索引是否小于使用 len() 函数(len() 方法返回对象中的项目数)的列表长度。

  • 如果条件为真,则使用pop()函数删除相应索引处的项目。

  • 删除给定索引处的项目后打印列表。

示例

inputList = ["Welcome", "to", "tutorialspoint", "python"] # Entering the indices list at which the items are to be deleted givenIndices = [1, 3] # Reversing Indices List indicesList = sorted(givenIndices, reverse=True) # Traversing in the indices list for indx in indicesList: # checking whether the corresponding iterator index is less than the list length if indx < len(inputList): # removing element by index using pop() function inputList.pop(indx) # printing the list after deleting items at the given indices print("List after deleting items at the given indices:\n", inputList)

输出

List after deleting items at the given indices : 
['Welcome', 'tutorialspoint']

结论

在本文中,我们学习了四种不同的方法来根据索引删除列表中的元素。我们还学习了如何使用 pop() 和 sorted() 函数从列表中删除多个索引元素。

更新于:2023-08-23

106K+ 次查看

启动您的 职业生涯

通过完成课程获得认证

开始
广告

© . All rights reserved.