Python List Insert () method detailed

Source: Internet
Author: User
Tags abs python list

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.