Python 列表 insert() 方法
實例
把值 "orange" 作為第二個元素插入 fruits 列表:
fruits = ['apple', 'banana', 'cherry'] fruits.insert(1, "orange")
定義和用法
insert() 方法在指定位置插入指定的值。
語法
list.insert(position, element)
參數值
參數 | 描述 |
---|---|
position | 必需。數字,指定在哪個位置插入值。 |
element | 必需。元素,任何類型(字符串、數字、對象等)。 |