python list insertion --append (), extend (), insert ()

append (), extend (), insert () operation is a list of commonly used functions is inserted. Wherein each receive a former two parameters, and inserted into the end of the list. The last two parameters received, the parameter 2 is inserted prior to a parameter.

This paper discusses the append () and extend () difference.

Both of which are inserted into the function, but the difference is , append () parameters are treated as objects, and parameter () extend the list is considered

 

In other words, append () function sets all other parameters as a list element inserted into the end of the list, look at an example

一、append()

Create an empty list, right

>>> empty = [] 

>>> empty

[]

1. Insert conventional digital

2. Insert the string

3. Insert List

 

 Then look at the length of

 

 Two, extend ()

Through observation, found that append () function regardless of what passed, only to be regarded as a list of elements were inserted into the tail

The extend () function with just the opposite, extend () function will pass the parameter as a list, and each element of the list one by one individually inserted into the original end of the list ,,, If this parameter is not a list, an error

1. The list of parameters passed in a non-error

 

 2. incoming string / list

 

 How, it is very interesting, extend directly to the "code" for each character as a list of elements to insert

So, if you want to insert a full string it

 

 Only need to string [] it indicates an enclosed list can, if you want to insert a plurality of one-time, only need to add the string to be inserted within []

Three, insert ()

First look the same function declarations

 

 Obviously this insert () is going to be passed in the object parameter as a list of elements to deal with

Look at the results

 

Guess you like

Origin www.cnblogs.com/GuoYuying/p/11518891.html