描述
這個insert()方法在偏移索引處將對象 obj 插入到列表中。
用法
以下是語法insert()方法 -
list.insert(index, obj)
參數
index- 這是需要插入對象 obj 的索引。
obj- 這是要插入給定列表的對象。
返回值
此方法不返回任何值,但會在給定索引處插入給定元素。
示例
下麵的例子展示了 insert() 方法的用法。
#!/usr/bin/python3
list1 = ['physics', 'chemistry', 'maths']
list1.insert(1, 'Biology')
print ('Final list:', list1)
結果
當我們運行上麵的程序時,它會產生以下結果 -
Final list: ['physics', 'Biology', 'chemistry', 'maths']
相關用法
- Python 3 List index()用法及代碼示例
- Python 3 List pop()用法及代碼示例
- Python 3 List len()用法及代碼示例
- Python 3 List extend()用法及代碼示例
- Python 3 List count()用法及代碼示例
- Python 3 List reverse()用法及代碼示例
- Python 3 List list()用法及代碼示例
- Python 3 List append()用法及代碼示例
- Python 3 List max()用法及代碼示例
- Python 3 List sort()用法及代碼示例
- Python 3 List min()用法及代碼示例
- Python 3 Number tan()用法及代碼示例
- Python 3 os.fstatvfs()用法及代碼示例
- Python 3 os.minor()用法及代碼示例
- Python 3 dictionary cmp()用法及代碼示例
- Python 3 String isupper()用法及代碼示例
- Python 3 os.close()用法及代碼示例
- Python 3 String decode()用法及代碼示例
- Python 3 os.unlink()用法及代碼示例
注:本文由純淨天空篩選整理自 Python 3 - List insert() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。