Summary of common Python list operations, pythonlist

Source: Internet
Author: User
Tags python list

Summary of common Python list operations, pythonlist

Common List object operation methods:

List. append (x)

Add an element to the end of the linked 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 linked list, and. insert (len (a), x) is equivalent to. append (x ).

List. remove (x)

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

List. pop ([I])

Delete an element from the specified position of the linked list and return it. If no index is specified, a. pop () returns the last element. The element is deleted from the linked 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 linked list. If no matching element exists, an error is returned.

List. count (x)

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

List. sort ()

Sort the elements in the linked list in the local area.

List. reverse ()

In-Place inverted list elements.

Example:
Copy codeThe Code is 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)
>>>
[66.25, 333,-1,333, 1, 1234.5, 333]
>>> A. index (333)
1
>>> A. remove (333)
>>> A. index (333)
2
>>>
[66.25,-1,333, 1, 1234.5, 333]
>>> A. reverse ()
>>>
[333,123 4.5, 1,333,-1, 66.25]
>>> A. sort ()
>>>
[-1, 1, 66.25, 333,333,123 4.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.