Python BASICS (list class)

Source: Internet
Author: User

Python BASICS (list class)
Iii. list (list class) Prompt: All of the following methods are methods in the class, the first parameter is self, and they are not written. Include: 1. append (x) # Add x to the end of List. >>> List = ['a'] >>> list. append ('B') >>> list ['A', 'B'] 2. extend (L) # append table L to the end and merge the two lists. >>> List = ['A', 'B'] >>> list2 = ['C', 'D'] >>> list. extend (list2) >>> list ['A', 'B', 'C', 'D'] 3. insert (I, x) # insert element x to the I position. >>> List. insert (0, 'test') ['test', 'A', 'B', 'C', 'D'] 4. remove (x) # Delete the first element in the list with the value of x. If it does not exist, an error occurs >>> list. remove ('test') >>> cannot be the location ['A', 'B', 'C', 'D'] 5. pop ([I]) # Delete and return the element whose position is I. If the parameter is omitted, the last element stack is deleted: LIFO (first-in-first-out) >>> list. pop () 'D' >>> list ['A', 'B', 'C'] >>> list. pop (1) 'B' >>> list ['A', 'C'] 6. index (I) # Return the position of the first element whose value is x, if not, an error occurs.> list. index ('A') 0 >>> list. index ('C') 1 7. count (x) # returns the number of elements equal to x> list ['A', 'C', 'A ', 'A', 'D'] >>> list. count ('A') 3 8. sort () # sort List elements by non-decreasing order. Another sorting method is sorted () function> list. sort () >>> list ['A', 'C', 'D'] 9. reverse () # reverse sorting of List elements> list. reverse () >>> list ['D', 'C', 'A', 'A', 'a']

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.