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']