List operations in Python

Source: Internet
Author: User

Portal Official file Address

list. Append (x):

Add x to the tail of the list, equivalent to A[len (a):] = [x]

Cases:

>>> list1=[1,2,3,4]>>> list1.append (5)>>> list1[1, 2, 3 , 4, 5]

list. Extend (L)

Adds an element from list L to list, equivalent to A[len (a):] = L.

Cases:

>>> list1=[1,2,3,4]>>> l=[5,6,7,8]>>> list1.extend (L) >>> list1[1, 2, 3, 4, 5, 6, 7, 8]

list. insert ( i , x )
The

Inserts the element at the specified location. The first parameter specifies which position to insert before the element. A.insert (0,x) is inserted at the front of the list, A.insert (Len (a), x) is equivalent to the A.append (x)

Example:

 >>>list1=[1,2,3,4]  >>> List1.insert (1,45)  >>>
  list1[ 1, 2, 3, 4] 
list. remove ( x )

Removes the first element of the list with a value of x, and returns error if no such element,
Example:

 >>& Gt List1=[1,2,3,4,5,1,2,3,4,5  >>> List1.remove (1,2) Traceback (most recent): File   <PYSHELL#80>  , Line 1, in  <module> List1.remove ( 1,2) Typeerror:remove () takes exactly one argument ( 2 given)  >>> list1.remove (1 >>> list1[ 2, 3, 4, 5, 1, 2, 3, 4, 5] 
list. Pop ( [ i])

Remove the item at the given position in the list, and return it. If No index is specified, a.pop () removes and returns the last item in the list. (The square brackets around the I in the method signature denote that the parameter are optional, not so you s Hould type square brackets at that position. You'll see this notation frequently in the Python Library Reference.)

list. Index ( x )

Return the index in the list of the first item whose value is x. It is a error if there is no such item.

list. Count ( x )

Return the number of times x appears in the list.

list. Sort ( cmp=none, key=none, reverse=false )

Sort the items of the list in place (the arguments can is used for sort customization, see sorted () for their E xplanation).

list. Reverse ( )

Reverse The elements of the list, in place.

A example that uses most of the list methods:

List operations in Python

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.