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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.