在Python中,`insert()`函数用于将一个元素插入到列表的指定位置。其基本语法如下:
list.insert(index, element)
其中:
`list` 是要插入元素的列表。
`index` 是要插入元素的位置(索引)。
`element` 是要插入到列表中的元素。
`insert()`函数会将元素插入到指定位置,并将该位置原有的元素及其后的元素依次往后移动。
例如,如果你有一个列表 `fruits`,并想在索引位置 `1` 插入元素 `'orange'`,你可以这样写:
fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, 'orange')
print(fruits) 输出: ['apple', 'orange', 'banana', 'cherry']
请注意,Python中的索引是从 `0` 开始的