Python:extend (extension) and append (append) between the days and the ground

Source: Internet
Author: User

1>>> Li = ['a','b','C']2>>> Li.extend (['D','e','F']) 3>>>Li4['a','b','C','D','e','F']5>>>Len (LI)667>>> li[-1]8 'F'9>>> Li = ['a','b','C']Ten>>> Li.append (['D','e','F'])  One>>>Li A['a','b','C', ['D','e','F']] ->>>Len (LI) -4 the>>> li[-1] -['D','e','F']
    • Lists's two methods extend and append look similar, but in fact they are completely different. Extend accepts a parameter, which is always a list, and adds each element in the list to the original list.
    • There are 3 elements (' A ', ' B ' and ' C ') in the list, and a second list with 3 elements (' d ', ' e ' and ' F ') is expanded, so there are 6 elements in the new list.
    • On the other hand, append accepts a parameter, which can be any data type, and is simply appended to the tail of the list. Call the Append method here using a list parameter containing 3 elements.
    • The list that originally contains 3 elements now contains 4 elements. Why is it 4 elements? Because the last element you just appended is a list. The list can contain any type of data, including other lists. This may be the result you want, maybe not. If your intent is extend, do not use append.

Python:extend (extension) and append (append) between the days and the ground

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.