Python insert()方法--list


描述

  • insert()方法:用於向列表中指定的索引位置之前插入新的對象,因為是在對應目標之前插入,故此方法無法像append()方法一樣將對象添加到列表末尾。

語法

  • 語法格式:list.insert(index, object)

參數

  • index:要插入的列表對象的索引位置。
  • object:插入到列表的對象。

返回值

  • 無返回值

實例

實例如下:

#!/usr/bin/python3

a = ['abc', '2019_11', 'pople']
others = {'name': 'jack'}

a.insert(-1, 'python')   # 在列表末尾之前插入字符串對象(無法添加新的對象到列表末尾)
a.insert(1, others)     # 在索引值為1的對象之前插入others	
print("New list: " + str(a))

輸出:

New list: ['abc', {'name': 'jack'}, '2019_11', 'python', 'pople']

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM