All methods of the Python list type

Source: Internet
Author: User
Tags python list
There are many methods for the list type. here, there are many methods for listing all methods of the list type. here, all methods of the list type are:

List. append (x)

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

List. extend (L)

Add all elements in a given list to another list, which is equivalent to a [len (a):] = L.

List. insert (I, x)

Insert an element at the specified position. The first parameter is the index of the element to be inserted before it, for example,. insert (0, x) is inserted before the entire list, and. insert (len (a), x) is equivalent to. append (x ).

List. remove (x)

Delete the first element whose value is x in the list. If no such element exists, an error is returned.

List. pop ([I])

Delete an element from the specified position in the list and return it. If no index is specified, a. pop () returns the last element. The element is deleted from the list. (Square brackets on both sides of method I indicate that this parameter is optional, rather than requiring you to enter a pair of square brackets. you will often encounter this mark in the Python library reference manual .)

List. index (x)

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

List. count (x)

Returns the number of times x appears in the list.

List. sort ()

Sort the elements in the list in place.

List. reverse ()

The elements in the reverse list.

The following example demonstrates most of the methods in the 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>>> 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]

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.