如何在 Python 中在列表的指定位置插入物件?


Python 中,列表 是一個有序的序列,可以容納多種物件型別,例如整數、字元或浮點數。在其他程式語言中,列表相當於陣列。

在本文中,我們將向您展示如何使用 Python 在列表的指定位置插入專案/物件。

  • 在列表的特定位置插入專案

  • 在列表的第一個位置插入專案

  • 在列表的最後/末尾位置插入專案

假設我們已經有一個包含一些元素的列表。我們將使用上面指定的不同方法,在列表的不同索引位置插入物件。

方法 1:在列表的特定位置插入專案

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入列表。

  • 輸入要插入列表的專案,並建立一個變數來儲存它。

  • 輸入必須插入專案的索引值。

  • 使用 insert() 函式(在指定位置插入提供的值)透過將索引值和要插入的專案作為引數傳遞給它,將給定的專案插入列表的指定索引。

list.insert(position, element)
  • 在將給定專案/物件插入到指定索引後列印列表。

示例

下面的程式使用 insert() 函式將給定專案插入到輸入的索引位置:

# input list lst = ["Hello", "TutorialsPoint", 20, "python", "code"] print("List =",lst) # giving the item to be inserted insertItem = "sample" # giving the index value at which the item to be inserted indexValue = 2 # inserting the given list item at the specified index(here 2) lst.insert(indexValue, insertItem) # printing the list after insertion print("The list after inserting the given list item at the specified index:") print("List = ",lst)

輸出

執行上述程式將生成以下輸出:

List = ['Hello', 'TutorialsPoint', 20, 'python', 'code']
The list after inserting the given list item at the specified index:
List = ['Hello', 'TutorialsPoint', 'sample', 20, 'python', 'code']

我們有一個包含一些隨機資料的示例列表。程式碼需要將必須新增到列表中的元素以及必須輸入該元素的索引。然後將索引和元素作為引數傳遞給 insert() 函式,並在新增元素後列印列表。

方法 2:在列表的第一個位置插入專案

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入列表。

  • 輸入要插入列表的專案,並建立一個變數來儲存它。

  • 使用 insert() 函式(在指定位置插入提供的值)透過將索引值設為 0 並將要插入的專案作為引數傳遞給它,將給定專案插入列表的第一個位置(index=0)。

lst.insert(0, insertItem)
  • 在將給定專案/物件插入到第一個位置(index=0)後列印列表。

示例

下面的程式使用 insert() 函式將給定專案插入列表的第一個位置:

# input list lst = ["Hello", "TutorialsPoint", 20, "python", "code"] print(lst) # giving the item to be inserted insertItem = "sample" # inserting the list item at the first position(index=0) in the list lst.insert(0, insertItem) # printing the list after insertion print("The list after inserting the list item at the first position(index=0) in the list:") print(lst)

輸出

執行上述程式將生成以下輸出:

['Hello', 'TutorialsPoint', 20, 'python', 'code']
The list after inserting the list item at the first position(index=0) in the list:
['sample', 'Hello', 'TutorialsPoint', 20, 'python', 'code']

由於我們需要在開頭插入元素,因此我們將索引設定為 0。

方法 3:在列表的最後/末尾位置插入專案

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入列表。

  • 輸入要插入列表的專案,並建立一個變數來儲存它。

  • 使用 insert() 函式(在指定位置插入提供的值)透過傳遞列表長度(要獲取列表的長度,我們將使用 len() 方法)和要插入的專案作為引數,將給定專案插入列表的末尾。

lst.insert(len(lst), insertItem)

    列表的最後一個索引是 len(list)-1,因此我們使用 len(list) 作為引數在末尾插入專案。

When this index is passed to the insert method, the element is inserted at the end of the list.
  • 在將給定專案/物件插入到列表末尾後列印列表。

示例

下面的程式使用 insert() 函式將給定專案插入列表的最後一個位置:

# input list lst = ["Hello", "TutorialsPoint", 20, "python", "code"] print(lst) # giving the item to be inserted insertItem = "sample" # inserting the list item at the end of the list # len(lst) gives the list length i.e, the last index value lst.insert(len(lst), insertItem) # printing the list after insertion print("The list after inserting the list item at the end:") print(lst)

輸出

執行上述程式將生成以下輸出:

The list after inserting the list item at the end:
['Hello', 'TutorialsPoint', 20, 'python', 'code', 'sample']

由於我們需要在末尾插入條目,因此我們將索引設定為列表的長度。

結論

在本程式碼中,我們學習瞭如何使用 insert() 函式在列表的開頭、指定索引和末尾插入元素。

更新於:2023年8月27日

36K+ 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.