Python List Method Summary

Source: Internet
Author: User

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

Append (...)

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

1234 >>> a = [ ' Sam ' 24 ' Shaw ' ] >>> a.append ( ' " >>> a [ ' Sam ' 24 Shaw ' " "

2. Find The number of value in list

Count (...)

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

123 >>> L =[12,‘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 (...)

12345 >>> L =[12,‘school‘,‘ball‘,24,12]>>> S =[‘haha‘,25,‘mail‘]>>> L.extend(S)>>> L[12, ‘school‘, ‘ball‘, 24, 12,‘haha‘, 25, ‘mail‘]

4. To insert the specified object into the list

Insert (Index,object)

1234 >>> L = [ 12 ' school ' 12 ] >>>l.insert ( 0 Shaw ' ) >>> L [ ' Shaw ' 12 ' 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 .

12345 >>> L = [ ' Shaw ' 12 ' school ' Code class= "Python plain", 12 ] >>> l.pop ()    # (delete the last one by default) Code class= "Python value" >12 >>> L.pop ( 0 ) ' Shaw '          # (delete first)

6.   detects if a substring contains substrings  str   beg (start) and  end (end)   str not in string

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.

1234567 >>> L = [‘shaw‘, 12,‘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.

1234567 >>> L = [‘shaw‘, 12,‘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 (...)

1234 >>> L = [ ' Shaw ' 12 ' school ' Code class= "Python plain" > >>> l.reverse () >>> L [ ' school ' , 12 ' 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)

1234 >>> L = [‘Shaw‘, 12,‘abc‘,24,‘biu‘,‘cd‘]>>> L.sort()>>> L[12, 24, ‘Shaw‘, ‘abc‘, ‘biu‘,‘cd‘]



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.