Python list (list) Common methods of operation summary _python

Source: Internet
Author: User
Tags python list

Common List Object action methods:

List.append (x)

Add an element to the end of the list, equivalent to A[len (a):] = [x].

List.extend (L)

Adds all the elements in a given list to another list, equivalent to A[len (a):] = L.

List.insert (i, X)

Inserts an element at the specified location. The first parameter is the index of the element that is ready to be inserted in front of it, for example, A.insert (0, X) is inserted into the entire list, and A.insert (Len (a), x) is the equivalent of A.append (x).

List.remove (x)

Deletes the first element in the list that has a value of x. If there is no such element, an error is returned.

List.pop ([i])

Deletes the element from the specified location in the linked list and returns it. If no index is specified, A.pop () returns the last element. The element is then deleted from the list. (The square brackets on both sides of the method indicate that this parameter is optional, rather than asking you to enter a pair of brackets, which you will often encounter in the Python Library Reference Manual.) )

List.index (x)

Returns the index of the element with the first value of x in the list. If there is no matching element, an error is returned.

List.count (x)

Returns the number of times X appears in the linked list.

List.sort ()

Sorts elements in a linked list in place.

List.reverse ()

In-place inverted elements in a linked list.

Cases:

Copy Code code as follows:

>>> a = [66.25, 333, 333, 1, 1234.5]
>>> Print A.count (333), A.count (66.25), A.count (' x ')
2 1 0
>>> A.insert (2,-1)
>>> A.append (333)
>>> A
[66.25, 333,-1, 333, 1, 1234.5, 333]
>>> A.index (333)
1
>>> A.remove (333)
>>> A.index (333)
2
>>> A
[66.25,-1, 333, 1, 1234.5, 333]
>>> A.reverse ()
>>> A
[333, 1234.5, 1, 333,-1, 66.25]
>>> A.sort ()
>>> A
[-1, 1, 66.25, 333, 333, 1234.5]

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.