Python List Method Summary

Source: Internet
Author: User
Tags python list

1. Add a new element to the tail of the list

Append (...)

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

>>> a = [' Sam ', ' Shaw ']

>>> a.append (' 35 ')

>>> A

[' Sam ', ', ' Shaw ', ' 35 ']

2. Find The number of value in list

Count (...)

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

>>> L = [N, ' school ', ' Ball ', 24,12]

>>> L.count (12)

2

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

Extend (...)

>>> L =[12, ' school ', ' Ball ', 24,12]

>>> S =[' haha ', +, ' mail ']

>>> L.extend (S)

>>> L

[School ', ' Ball ', ' V ', ' haha ', ' mail ']

4. To insert the specified object into the list

Insert (Index,object)

>>> L =[12, ' school ', 12]

>>>l.insert (0, ' Shaw ')

>>> L

[' Shaw ', ' School ', 12]

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

Pop (...)

Description:

l.pop ([index]), item--Remove Andreturn item at index (default last). raisesindexerror If list is empty or index was out of range .

>>> L = [' Shaw ', ' School ', 12]

>>> L.pop () (the last one is deleted by default)

12

>>> L.pop (0)

' Shaw ' (delete the first one)

6. detects if the string contains substrings of str , if beg(start) and end are specified (end) scope, the check is contained within the specified range, and the method is python find () method, just as if Str not in string an exception is reported in the

Str.index (str, beg=0, End=len (String))

Parameters:

STR-- specifies the retrieved string

Beg-- start index, default to 0 .

End-- The end index, which defaults to the length of the string.

>>> L = [' Shaw ', ' School ', 12]

>>> l.index (' Shaw ')

0

>>> l.index (' Sam ')

Traceback (most recent Calllast):

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

ValueError : ' Sam ' isnot in list

7. used to remove the first matching element of a value in the list.

L.remove (value)

Raises ValueError If the value is not present.

>>> L = [' Shaw ', ' School ', 12]

>>> L.remove (12)

>>> L

[' Shaw ', ' School ', 12]

>>> L.remove (12)

>>> L

[' Shaw ', ' School ']

8. used to reverse the elements in the list (reverse-order the elements of the list)

Reverse (...)

>>> L = [' Shaw ', ' School ']

>>> L.reverse ()

>>> L

[' School ', ' Shaw ']

9. used to sort the original list, if a parameter is specified, use the comparison function specified by the comparison function. ( sort value in list (first digit, in uppercase letters, lowercase letters))

L.sort (Cmp=none, Key=none, Reverse=false)

>>> L = [' Shaw ', ' n ', ' abc ', ' Biu ', ' CD ']

>>> L.sort ()

>>> L

[+, ' Shaw ', ' abc ', ' Biu ', ' CD ']


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

Python List Method Summary

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.