Python3 List insert() Method
Description
The insert() function is used to insert a specified object at the specified position in a list.
Syntax
The syntax of the insert() method is:
list.insert(index, obj)
Parameters
- index -- The index position where the object obj needs to be inserted.
- obj -- The object to be inserted into the list.
Return Value
This method has no return value, but it inserts the object at the specified position in the list.
Example
The following example demonstrates how to use the insert() function:
Example
#!/usr/bin/python3
list1 = ['Google', 'Example', 'Taobao']
list1.insert(1, 'Baidu')
print ('The list after inserting the element is : ', list1)
list1 = ['Google', 'Example', 'Taobao']
list1.insert(1, 'Baidu')
print ('The list after inserting the element is : ', list1)
The output of the above example is:
列表插入元素后为 : ['Google', 'Baidu', 'Example', 'Taobao']Other Extensions
Python3 Lists