Python-extend v append on a list (comparison between extend and append in the list in Python)

Source: Internet
Author: User

Comparison between extend and append in list in Python

What's the difference? If you append a list to another list, you add the new list as a single extra list to the original, thus makingthe original list just one longer with an item that is itself a list. but if you extend a list with another list, you add each element of the new list onto the original. here's an example to show you what I mean:

>>> First = [10, 20, 30]
>>> Second = [40, 50, 60]
>>> First. append ([70, 80, 90])
>>> Second. Extend ([2, 100,110,120])
>>> First
[10, 20, 30, [70, 80, 90]
>>> Second
[40, 50, 60,100,110,120]
>>>

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.