All methods of the Python list type

Source: Internet
Author: User
Tags python list
There are many ways to list types, and here are all the methods of the list type:

List.append (x)

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

List.extend (L)

Adds all the elements from 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 before it, such as A.insert (0, X) is inserted before the entire list, while A.insert (Len (a), x) is equivalent to A.append (x).

List.remove (x)

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

List.pop ([i])

Removes the element from the specified position in the list and returns it. If no index is specified, A.pop () returns the last element. The element is removed 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 parentheses, you will often encounter such tags in the Python Library Reference Manual.) )

List.index (x)

Returns the index of the first element in the list that has a value of x. If there are no matching elements, an error is returned.

List.count (x)

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

List.sort ()

Sorts the elements in the list in place.

List.reverse ()

The elements in the table are inverted in place.

The following example shows most of the methods of a list

>>> 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& Gt;>> A.remove (333) >>> 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]
  • Related Article

    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.