"Author's Homepage": Shibie Sanri wyx
"Author's Profile": CSDN top100, Alibaba Cloud Blog Expert, Huawei Cloud Share Expert, Network Security High-quality Creator
"Recommended Column": Xiaobai Zero Basic "Python Beginner to Master"
Detailed explanation of insert function
insert() can "insert" the object into the "specified position" of the list
grammar
list.insert( index, obj )
parameter
- index : (required) index position
- obj : (optional) the object to insert
Example: inserting content into a list
list1 = [1, 2, 3]
list1.insert(1, 'a')
print(list1)
output:
[1, 'a', 2, 3]
1. Insertion position
Set the "index" to specify the "position" of the object insertion . The index has some special values that need to be noted.
1.1. Positive index
When the index is a "positive number" , the index starts from 0 and counts "from left to right" .
For example, if the index is 0, it will be inserted at the first position on the left; if the index is 1, it will be inserted at the second position on the left; and so on
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list1.insert(0, 'a')
print(list1)
list2.insert(1, 'a')
print(list2)
output:
['a', 1, 2, 3]
[1, 'a', 2, 3]
When the index "exceeds" the "length" of the list , no error will be reported, but it will be inserted to the far right of the list
list1 = [1, 2, 3]
list1.insert(10, 'a')
print(list1)
output:
[1, 2, 3, 'a']
1.2. Negative index
When the index is a "negative number" , the index starts from 0 and counts "from right to left" .
For example, if the index is -1, it is inserted at the second position on the right; if the index is -2, it is inserted at the third position on the right; and so on
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list1.insert(-1, 'a')
print(list1)
list2.insert(-2, 'a')
print(list2)
output:
[1, 2, 'a', 3]
[1, 'a', 2, 3]
When the index "exceeds" the "length" of the list , no error will be reported, but it will be inserted to the far left of the list
list1 = [1, 2, 3]
list1.insert(-10, 'a')
print(list1)
output:
['a', 1, 2, 3]
It should be noted here that 0 does not distinguish between positive and negative, and the way of negative index can only be inserted from the second position on the right, because there is already a function such as append() or extend() that is added to the end of the list by default.
2. Insert the object
The object inserted by insert() can be "any type" .
"Integer" and "String" types will be inserted as a new element
list1 = [1, 2, 3]
list1.insert(1, 'a')
print(list1)
list1.insert(1, 9)
print(list1)
output:
[1, 'a', 2, 3]
[1, 9, 'a', 2, 3]
"List" , "Yuanzu" and "dictionary" , not every element will be inserted, but the "whole" will be inserted as an element
list1 = [1, 2, 3]
list2 = [4, 5]
tuple2 = ('a', 'b')
dict2 = {
'key1': 1, 'key2': 2}
str2 = 'abc'
list1.insert(1, list2)
print(list1)
list1.insert(1, tuple2)
print(list1)
list1.insert(1, dict2)
print(list1)
list1.insert(1, str2)
print(list1)
output:
[1, [4, 5], 2, 3]
[1, ('a', 'b'), [4, 5], 2, 3]
[1, {
'key1': 1, 'key2': 2}, ('a', 'b'), [4, 5], 2, 3]
[1, 'abc', {
'key1': 1, 'key2': 2}, ('a', 'b'), [4, 5], 2, 3]