Road learning Python: List (List) of append (), extend () and insert () method

Same point

The role of these three methods are for the list (List) to add value

Their syntax is:

List II.A PPEND ( obj )
List . Extend (SEQ )
list.insert (index, obj) # where index is the index object obj to be inserted position

 

difference

For the convenience of elaboration, create the following list:

= A [. 1 ] List # A 
B = [22,333] # If not noted here list, which may be a single value or a sequence of any type of

 

Three methods were performed and the results observed:

a.append (B)
 Print (A) 

the result is: 
[ 1, [22, 333]]
a.extend (B)
 Print (A) 

the result is: 
[ 1, 22, 333]
a.insert (0, B)
 Print (A) 

the result is: 
[[ 22, 333], 1]

Observed following conclusions can be obtained, these three different methods as follows:

 

1, the value added at different locations

 append () and extend () method can only be added to the list of values of the end , the insert () method can be inserted into the list of values of an arbitrary position

2, when the added value of different types of treatment

append () and insert () method b is the types and values are added to the added to the list a, and extend () method except that the b value is added to a list of

Guess you like

Origin www.cnblogs.com/toxic-antidote/p/11408741.html