Python List Method Summary

Source: Internet
Author: User
Tags iterable python list

1. to add a new element ( object ) at the end of the list

L.append (object)--Append object to end

>>>l = [' Sam ', ' Shaw ', ' Stiven ']

>>>l

[' Sam ', ' Shaw ', ' Stiven ']

>>>l.append (' Alice ')

>>>l

[' Sam ', ' Shaw ', ' Stiven ', ' Alice ']

2. used to count the number of occurrences of an element in a list.

L.count (value), integer--Returnnumber of occurrences of value

>>>l = [' Sam ', ' Amy ', ' Miya ', ' Sam ']

>>>l

[' Sam ', ' Amy ', ' Miya ', ' Sam ']

>>>l.count (' Sam ')

2

>>>l.count (' Amy ')

1

3 . Used to append multiple values from another sequence at the end of the list (extending the original list with a new list)

L.extend (iterable)--Extend list byappending elements from the iterable

>>>l = [' Sam ', ' Amy ']

>>>y = [123, ' Boy ']

>>>l.extend (y)

>>>l

[' Sam ', ' Amy ', 123, ' Boy ']

4. used to find the index position of the first occurrence of a value from a list

L.index (value, [Start, [stop]])->integer--return first index of value

raises valueerror ifthe value is not present.

>>> L = [' Sam ', ' Amy ', ' Miya ']

>>> l.index (' Amy ')

1

5. To insert the specified object into the list

L.insert (Index, object)--Insert Object Beforeindex

Index-- Object Object the index location to insert

Object-- to access objects in the list

>>>l = [' Sam ', ' Amy ', ' Miya ']

>>>l.insert (2, ' Koko ')

>>>l

[' Sam ', ' Amy ', ' Koko ', ' Miya ']

6 . Used to remove an element from the list (the last element by default), and returns the value of the element

L.pop ([index]), item--Remove Andreturn item at index (default last).

Raises indexerror if List IsEmpty or index is out of range.

>>>l = [' Sam ', ' Amy ', ' Miya ', ' Sam ']

>>>l.pop ()

' Sam '

>>>l

[' Sam ', ' Amy ', ' Miya ']

>>>l.pop (1)

' Amy '

>>>l

[' Sam ', ' Miya ']

7. To remove the first occurrence of a value in a list

L.remove (value)--Remove first occurrenceof value.

raises valueerror ifthe value is not present.

>>> L = [123, ' xyz ', ' Zara ', ' abc ', ' XYZ ']

>>> l.remove (' xyz ')

>>>l

[123, ' Zara ', ' abc ', ' XYZ ']

>>>l.remove (' Shaw ')

Traceback (most recent):

File "<input>", line 1, in <module>

ValueError:list.remove (x): X not in List

8. used to reverse the elements in the list (the method does not return a value, but reverses the order of the elements of the list)

L.reverse ()--Reverse *in place*

>>>l = [' Shaw ', ' Sam ', ' Alice ']

>>>l.reverse ()

>>>l

[' Alice ', ' Sam ', ' Shaw ']

9. sort value in list (first digit, in uppercase, lowercase), if specified, use comparison function specified by comparison function

L.sort ([func])

Func-- Optional Parameters , If this parameter is specified, the method of the parameter is used to sort

>>>l = [' branch ', ' Sam ', ' Amy ']

>>>l.sort ()

>>>l

[' Amy ', ' branch ', ' Sam ']

>>>l = [8, ' branch ', ' A ', ' Sam ', ' Amy ', 6]

>>>l.sort ()

>>>l

[6,8, ' A ', ' Amy ', ' branch ', ' Sam ']


This article is from the "Shaw blog" blog, please be sure to keep this source http://opsedu.blog.51cto.com/9265055/1761216

Python List Method Summary

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.