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

35K+ 瀏覽量

啟動您的職業生涯

完成課程後獲得認證

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