如何在 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.