List (list) append () method in Python and the use and difference of the Extend () method

Source: Internet
Author: User
Tags stdin

The Append () method uses

First look at the description in the official documentation:

List.extend (L) Extend the list by appending all the items in the given list; Equivalent to A[len (a):] = L.

The translation into Chinese is: by appending all elements to the known list to augment it, equivalent to A[len (a):]= L

For example, I can understand this sentence better.

>>> la[1, 2, 3]>>> lb[‘qiwsir‘, ‘python‘]>>> la.extend(lb)>>> la[1, 2, 3, ‘qiwsir‘, ‘python‘]>>> lb[‘qiwsir‘, ‘python‘]

如果有两个list,一个是la,另外一个lb,将lb追加到la的后面,也就是把lb中的所有元素加入到la中,
即让la扩容。看代码
>>> LA = [1,2,3]>>> B ="ABC">>> la.extend (b) >>> la[ 1, 2, 3,  ' A ',  ' C ']>>> c = 5>>> la.extend (c) Traceback ( Most recent call last) : file  "< Stdin> ", line 1, in <module> typeerror:  ' int ' object is not iterable        

Originally, if extend (str), Str was opened in characters, and then appended to LA. If the Extend object is numeric, an error is obtained.

So, the Extend object is a list, and if it is STR, Python will first convert it to a list by character and append to the known list.





>>> la[1, 2, 3, ‘a‘, ‘b‘, ‘c‘]>>> lb[‘qiwsir‘, ‘python‘]>>> la[len(la):]=lb>>> la[1, 2, 3, ‘a‘, ‘b‘, ‘c‘, ‘qiwsir‘, ‘python‘]
Why the input la appears, after running [1, 2, 3, ‘a‘, ‘b‘, ‘c‘, ‘qiwsir‘, ‘python‘] 因为 list.extend (l) is equivalent to List[len (list):] = L, L is the list to be merged.




The Extend () method uses
>>> new = [1,2,3]>>> lst = [‘python‘,‘qiwsir‘]>>> lst.extend(new)>>> lst[‘python‘, ‘qiwsir‘, 1, 2, 3]>>> new[1, 2, 3]

With the Extend function, each element in [three-way] is taken out and then plugged into the LST, resulting in a different list than the original object element, followed by three more elements than the original.

Also, as you can see from the demo above, LST becomes a "new" list after the Extend function operation. This sentence seems a bit awkward, "seemingly new", the reason is that because of the "new" may have a different understanding. You might as well dig a bit deep.

>>> new = [1,2,3]>>> id(new)3072383244L>>> lst = [‘python‘, ‘qiwsir‘]>>> id(lst)3069501420L

Use the id() number of "nests" that can see two lists in memory, respectively.

>>> lst.extend(new)>>> lst[‘python‘, ‘qiwsir‘, 1, 2, 3]>>> id(lst)3069501420L

Did you notice? Although LST after the extend() method, compared to the original expansion, but did not leave the original "nest", that is, in memory, or "old", but the contents of the increase. The equivalent of two family, after some sex, and added a baby, then this home is "new" or "old" it? Different angles may differ.

This is an important feature of the list : The list can be modified. This modification, instead of copying a new one, modifies it in situ.

In fact, the operation of the append() list is the same, may wish to look at the same way.

Note: Although the LST content here is the same as above, I enter it from the shell, so the ID will change. That is, the memory allocation of the "nest" number has changed.

>>> lst = [‘python‘,‘qiwsir‘]>>> id(lst) 3069501388L>>> lst.append(new)>>> lst[‘python‘, ‘qiwsir‘, [1, 2, 3]]>>> id(lst)3069501388L

Obviously, the append() list is also modified in situ.

What if, for extend() , not providing a Iterable type object?

>>> lst.extend("itdiffer")>>> lst[‘python‘, ‘qiwsir‘, ‘i‘, ‘t‘, ‘d‘, ‘i‘, ‘f‘, ‘f‘, ‘e‘, ‘r‘]

It converts a string "Itdiffer" to [' I ', ' t ', ' d ', ' I ', ' f ', ' f ', ' e ', ' R '), and then provides the list as a parameter to extend, and plugs the elements of the list into the original list.

>>> num_lst = [1,2,3]>>> num_lst.extend(8)Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: ‘int‘ object is not iterable

This is an error. The error message tells us that the number 8, is an int type object, not iterable.

Here are the two functions that allow the list to be enlarged append() extend() . From the above demo, you can see that they have the same place:

    • It's all in place. Modify List
    • Since it is modified in situ, it does not return a value

You cannot assign a value to a variable if you modify it without a return value.

>>> one = ["good","good","study"]>>> another = one.extend(["day","day","up"]) #对于没有提供返回值的函数,如果要这样,结果是:>>> another #这样的,什么也没有得到。>>> one[‘good‘, ‘good‘, ‘study‘, ‘day‘, ‘day‘, ‘up‘]

So what's different about the two? Look at the following example:

>>> LST = [1,2,3]>>> Lst.append (["Qiwsir","GitHub"])>>> lst[1,2, 3, [ ' Qiwsir ',  #append的结果 >>> Len (LST) 4 >>> lst2 = [1,2,3]>>> lst2.extend ([ "Qiwsir",  "GitHub"]) >>> lst2[1, 2, 3,  ' Qiwsir ',  GitHub  #extend的结果 >>> Len (lst2)  5                 

Append is the whole establishment of the additional, extend is the individual expansion.

Extend its arguments as List,extend's behavior is to put the two list together, append is to consider its parameters as an element, as a whole added up.
The list can have arbitrary data types, so distinguish between the two functions.

< Span class= "Hljs-number" > &NBSP;

  
















br>
























































br>














































/span>

The use and difference of the list (list) append () method and the Extend () method in

Python

Related Article

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.