Extend () and append () differences

Source: Internet
Author: User

Both extend () and append () can be used to add.

Cases:

A = [1,2,3,4,5,6]

b = [7,8,9,10]

c = {' AA ': 123, ' BB ': 456, ' CC ': 789}

1. A.append (b)

Operation Result:

>>>[1,2,3,4,5,6,[7,8,9,10]]

2. A.extend (b)

Operation Result:

>>>[1,2,3,4,5,6,7,8,9,10]

3. A.extend (c)

>>>[1,2,3,4,5,6, ' AA ', ' BB ', ' CC '

A.append (c)

>>>[1,2,3,4,5,6,{' AA ': 123, ' BB ': 456, ' CC ': 789}]

By running the results above you can see that append () is looking at B as a whole and adding it to the list, while extend () takes each element of the list and adds it to table A.

When you add a dictionary, the dictionary key is added with extend (), and append () adds the entire dictionary C

Thank you for reading.

This article refers to:

Https://www.cnblogs.com/subic/p/6553187.html

Extend () and append () differences

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.