In python, the difference between append () of list type and extend () is: pythonappend

Source: Internet
Author: User

In python, the difference between append () of list type and extend () is: pythonappend

The list types in python are commonly used: append () function and extend () function. The two functions are similar. What is the difference between them?


1. append ()

Append () usage example: >>> mylist = [1, 2, 0, 'abc'] >>> mylist [1, 2, 0, 'abc'] >>> mylist. append (4) >>> mylist [1, 2, 0, 'abc', 4] >>> mylist. append ('hahaha') >>> mylist [1, 2, 0, 'abc', 4, 'hahaha']


2. extend ()


Extend () usage example: >>> mylist [1, 2, 0, 'abc', 4, 'hahaha'] >>> mylist. extend (['lulu']) >>> mylist [1, 2, 0, 'abc', 4, 'haha', 'lulu'] >>> mylist. extend ([aaa, 'lalala ']) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'aaa' is not defined >>> mylist. extend (['123', 'lalala '])> mylist [1, 2, 0, 'abc', 4, 'hahaha', 'lulu ', '200', 'lalala '] >>> mylist. extend ([111111,222]) >>> mylist [1, 2, 0, 'abc', 4, 'hahaha', 'lulu', '123', 'lalala ', 111111,222]


Analysis shows that:

Append () can only add one element to the list;

Extend () can only add another list.




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.