Common python list operations, python operations
This section describes how to operate a List in Python, including creating, accessing, updating, deleting, and other operations. For more information, see.
1. Create a list. You only need to enclose different data items separated by commas (,) in square brackets.
1 List = ['wade','james','bosh','haslem']
Like the string index, the List Index starts from 0. List can be intercepted, combined, etc.
2. Add new elements
1 List. append ('allen ') # Method 1: add the parameter object 2 >>> a = [1, 2, 4] 3 >>>. append (5) 4 >>> print (a) 5 [1, 2, 3, 4, 5] 6 7 List. insert (4, 'Lewis ') # Method 2: insert an element parameter 1: index location parameter 2: object 8 >>> a = [1, 2, 4] 9>. insert (2, 3) 10 >>> print (a) 11 [1, 2, 3, 4] 12 13 List. extend (tableList) # method 3: extended list, parameter: iterable parameter 14 >>> a = [1, 2, 3] 15 >>> B = [4, 5, 6] 16 >>>. extend (B) 17 >>> print (a) 18 [1, 2, 3, 4, 5, 6]
3. traverse the list
1 for i in List:2 print i,
4. Values in the access list
Use subscript indexes to access values in the list. You can also use square brackets to intercept characters, as shown below:
1 >>> List = [1, 2, 3, 4, 5, 6, 7 ]2 >>> print(List[3])3 4
5. Delete elements from the list
1 List. remove () # Delete Method 1: If the object has repeated elements, only the top 2 >>> a = [1, 2, 3] 3 >>>. remove (2) 4 >>> print (a) 5 [1, 3] 6 7 List. pop () # Delete Method 2: pop optional parameter index Delete the element at the specified position is the last element by default 8 >>> a = [1, 2, 3, 4, 5, 6] 9>. pop () 10 611 >>> print (a) 12 [1, 2, 3, 4, 5] 13 14 15 del List # Delete Mode 3: you can delete the entire list or specify elements or list slices. The list cannot be accessed after deletion. 16 >>> a = [1, 2, 3, 4, 5, 6] 17 >>> del a [5] 18 >>> print (a) 19 [1, 2, 3, 4, 5] 20 >>> del a21 >>> print (a) 22 Traceback (most recent call last): 23 File "<pyshell #93> ", line 1, in <module> 24 print ()
6. sort and reverse code
1 List. reverse () 2 >>> a = [1, 2, 3, 4, 5, 6] 3 >>>. reverse () 4 >>> print (a) 5 [6, 5, 4, 3, 2, 1] 6 7 8 List. sort () # sort has three default parameters cmp = None, key = None, and reverse = False. Therefore, you can set the sorting parameter 9 >>> a = [2, 4, 6, 7, 3, 1, 5] 10 >>>. sort () 11 >>> print (a) 12 [1, 2, 3, 4, 5, 6, 7] 13 # In python3X, numbers and characters cannot be sorted together, the error 14 >>> a = [2, 4, 6, 7, 3, 1, 5, 'a'] 15 >>>. sort () 16 Traceback (most recent call last): 17 File "<pyshell #104>", line 1, in <module> 18. sort () 19 TypeError: unorderable types: str () <int ()
7. truncate the Python list
1. the Python list truncation is of the same type as the string operation, as shown below:2 L = ['spam', 'spam', 'spam! '] 3 operation: 4 Python expression result description 5 L [2]' SPAM! 'Read the third element 6 L [-2] 'spam' in the list to read the second and last Elements 7 L [1:] ['spam', 'spam! '] Truncates the list from the second element.
8.PythonList operation functions and methods
1List operations include the following functions:2 1. cmp (list1, list2): Compare the elements of two lists (python3 discarded) 3 2. len (list): number of list elements 4 3. max (list ): returns the list element maximum value 5 4, min (list): returns the list element minimum value 6 5, list (seq): converts the tuples to the list
1List operations include the following methods:2 1. list. append (obj): Add new objects 3, list at the end of the list. count (obj): count the number of times an element appears in the list. extend (seq): append multiple values in another sequence at a time at the end of the list (extend the original list with the new list) 5. list. index (obj): locate the index position of the first matching item of a value from the list. insert (index, obj): insert objects to list 7, list. pop (obj = list [-1]): removes an element from the list (the last element by default) and returns the values of this element 8 and list. remove (obj): remove the first matching item 9 8 and list of a value in the list. reverse (): Elements 10 9 and list in the reverse list. sort ([func]): sorts the original list.