SoFunction
Updated on 2024-11-16

Three operations on adding elements to python lists

1, insert method, the method contains two parameters, the first parameter for the insertion of the location parameters, the second parameter for the insertion of content

a = [0,0,0]
b = [1,2,3]
(0,b)
print a

Output:

[[1, 2, 3], 0, 0, 0] 

2, extend method, the method's parameters for a list, the index specified to the list will be inserted into the list of original

a = [0,0,0]
b = [1,2,3]
(b)
print a

Output:

[0, 0, 0, 1, 2, 3]

3、append method, the method can only be followed by a parameter

a = [0,0,0]
(1)
print a

Output:

[0, 0, 0, 1]

Addendum: See below Python list-adding elements

Add element at the end

names = ['John','Thomas','Jack','Tony']

print(names)

# Add elements at the end
('Bill')
print(names)

program output

['John', 'Thomas', 'Jack', 'Tony']
['John', 'Thomas', 'Jack', 'Tony', 'Bill']

inserted element

names = ['John','Thomas','Jack','Tony']

print(names)

# Insert elements into the list


(0,'Bill')



print(names)

program output

['John', 'Thomas', 'Jack', 'Tony']
['Bill', 'John', 'Thomas', 'Jack', 'Tony']

summarize

The above is a small introduction to the python list to increase the elements of several methods of operation, I hope to help you, if you have any questions please leave me a message, I will promptly reply to you. Here also thank you very much for your support of my website!