Also talk about the difference between Python list append and extend

Source: Internet
Author: User
Tags iterable python list

On the two methods of append and extend, many people on the web say the following:

The append () method adds a new element to the tail of the list. Only one parameter is accepted.

The Extend () method takes only one list as an argument and adds each element of the parameter to the original list.

However, the argument that extend only accepts a list as a parameter is deeply doubtful. Give me a chestnut:

Extend>>>a= [1,2,3,4]>>> a.extend ([' A ', ' B ']) >>> a[1, 2, 3, 4, ' A ', ' B ']append>>> A.append ([' A ', ' B ']) >>> a[1, 2, 3, 4, ' A ', ' B ', [' A ', ' B ']


OK, so we have to append a list is no problem, change a tuple can?

Append>>> A.append ((c)) >>> a[1, 2, 3, 4, ' A ', ' B ', [' A ', ' B '], (1, 2, 3)]>>> a.extend ((4,5, 6) >>> a[1, 2, 3, 4, ' A ', ' B ', [' A ', ' B '], (1, 2, 3), 4, 5, 6]


Still can, tuples can also be accepted by extend, and successfully appended to the list, change the dictionary?

Extend>>> A.extend ({1: ' A ', 2: ' B '}) >>> a[1, 2, 3, 4, ' A ', ' B ', [' A ', ' B '], (1, 2, 3), 4, 5, 6, 1, 2]append  >>> A.append ({7: ' C ', 5: ' d '}) >>> a[1, 2, 3, 4, ' A ', ' B ', [' A ', ' B '], (1, 2, 3), 4, 5, 6, 1, 2, {5: ' d ', 7: ' C '}]


Here we can find a little different, extend way to expand the list parameter as a dictionary, the only key,value that are appended to the list are not appended, but when extended using append this way, the entire dictionary is appended to the list as a value.

OK, these are mutable objects, what if we replace them with strings?

Extend>>> a.extend (' 123456 ') >>> a[1, 2, 3, 4, ' A ', ' B ', [' A ', ' B '], (1, 2, 3), 4, 5, 6, 1, 2, {5: ' d ', 7  : ' C '}, ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ']append>>> a.append (' 123456 ') >>> a[1, 2, 3, 4, ' A ', ' B ', [' A ', ' B '], (1, 2, 3), 4, 5, 6, 1, 2, {5: ' d ', 7: ' C '}, ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 123456 ']


is also possible.

Summarize:

1, append and extend can accept various types of data, but when the extend parameter is a dictionary, only the key is appended to the list.

2, append and extend only accept one parameter

3, append accept a parameter, append to the list to add only one element at the end, that is, what you parameter, the list is appended to what.

4, extend accept a parameter, append to the list when each value in the parameter is appended to the list (dictionary exception)

Additional Help information

Help on built-in function extend:

Extend (...) method of Builtins.list instance

L.extend (iterable), None-- extend list by appending elements from the iterable

Does not say that the extend parameter must be a list.

Append an extension list from an iterative element

The above is purely personal experimental results, such as the great God has a better explanation, please answer a lot, thank you!


This article from the "Wind of the Crane" blog, declined to reprint!

Also talk about the difference between Python list append and extend

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.