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


在这篇文章中,我们将向您展示如何使用Python根据索引删除列表中的元素。我们将介绍四种方法来完成此任务:

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

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

  • 使用切片删除列表元素

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

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

方法一:使用 `del` 关键字删除列表元素

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

算法(步骤)

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

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

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

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

语法

"del list object [index]"

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

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

示例

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

Open Compiler
# 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']

方法二:使用 `pop()` 函数删除列表元素

算法(步骤)

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

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

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

示例

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

Open Compiler
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']

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

方法三:使用切片删除列表元素

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

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

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

  • 索引 N 位置的项目

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

算法(步骤)

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

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

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

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

示例

Open Compiler
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']

方法四:删除列表中的多个索引元素

算法(步骤)

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

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

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

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

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

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

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

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

示例

Open Compiler
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年8月23日

106K+ 次浏览

启动您的职业生涯

完成课程获得认证

开始学习
广告