Tag: Represents the Pre instance function Insert Object nbsp BSP Color Pytho
1. function
The insert () function is used to insert the specified object into the specified position of the list.
2. Syntax
List.insert (index, obj)
3. Parameters
Index: Object obj needs an indexed position to insert.
OBJ: Inserts an object from the list.
There are 5 different scenarios:
When scene 1:index=0, insert obj from head
When scene 2:index > 0 and Index < len (list), insert obj at index position
Scenario 3: When index < 0 and ABS (index) < Len (list), insert obj from the middle, such as:-1 to insert obj from the bottom 1th bit; 2 indicates the insertion of obj from the 1th-lowest position
Scenario 4: When index < 0 and ABS (index) >= len (list), insert obj from head
Scenario 5: When index >= len (list), insert obj from the tail
4. Return value
The method does not return a value, but inserts the object at the specified position in the list.
5. Example
>>> LST = [1,2,3,4,5]#Create a list>>> Lst.insert (0,0)#from the 1th position of the list, insert element 0--Scene 1>>>Lst[0,1, 2, 3, 4, 5]>>> Lst.insert (6,7)#from the 6th position of the list, insert element 7--Scene 2>>>Lst[0,1, 2, 3, 4, 5, 7]>>> Lst.insert ( -1,6)#Insert Element 6--Scene 3 from List-1 positions>>>Lst[0,1, 2, 3, 4, 5, 6, 7]>>> Lst.insert ( -20,10)#Insert Element 10--Scene 4 from list-20 positions>>>lst[10, 0, 1, 2, 3, 4, 5, 6, 7]>>> Lst.insert (30,20)#from the 30th position of the list, insert element 20--Scene 5>>>lst[10, 0, 1, 2, 3, 4, 5, 6, 7, 20]
Python List Insert () method detailed