Append () and extend () usage in Python list

Source: Internet
Author: User
Tags python list

Lists are implemented in the form of classes. The Create list is actually an instantiation of a class. Therefore, there are several ways to manipulate the list.
1. The list can contain elements of any data type, and the elements in a single list do not need to be all of the same type.
2. The append () method adds a new element to the tail of the list. Only one parameter is accepted.
3. The Extend () method accepts only one list as an argument and adds each element of the parameter to the original list.

Append () Usage Example:

MyList = [1,2,0, ' abc ']
MyList
[1, 2, 0, ' abc ']
Mylist.append (4)
MyList
[1, 2, 0, ' abc ', 4]
Mylist.append (' haha ')
MyList
[1, 2, 0, ' abc ', 4, ' haha ']

Extend () Usage Example:

MyList
[1, 2, 0, ' abc ', 4, ' haha ']
Mylist.extend ([' Lulu '])
MyList
[1, 2, 0, ' abc ', 4, ' haha ', ' Lulu ']
Mylist.extend ([AAA, ' Lalalala '])
Traceback (most recent):
File "", Line 1, in
Nameerror:name ' AAA ' is not defined
Mylist.extend ([' 123123 ', ' Lalalala '])
MyList
[1, 2, 0, ' abc ', 4, ' haha ', ' Lulu ', ' 123123 ', ' Lalalala ']
Mylist.extend ([111111,222])
MyList
[1, 2, 0, ' abc ', 4, ' haha ', ' Lulu ', ' 123123 ', ' Lalalala ', 111111, 222]

over!

Append () and extend () usage in Python 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.