Python list and pythonlist
Operation
1. Add
Append: adds an element to the end of the list.
Insert (n, element value): Add an element to the specified index
2. Delete
Pop (n): WHEN n is specified, the element at n is deleted. If n is not specified, the last element is deleted.
Del list [n]: delete a given element directly.
List. remove (obj)
Removes the first matching item of a value from the list.
3. Modify and query
You can directly access and modify a single element through subscript
A [-1] indicates the last element of the list, that is, the last element of the list.
A = [1, 2, 3, 4, 5]
A [:], a [:] is equivalent to a [0: n: 1]: used to access the elements from 0 to n-1 in the auto-increment mode, the number of elements is n, which is actually to access the entire list itself.
This has many functions, such
Only an odd number of items in the list are obtained using a [: 2].
An odd number element a starting from the third element [2: 2]
The element of the even number is a [1: 2].
The reverse order of the list is a [:-1], which is equivalent to a [-1:-1]. It refers to access from the last element of the list in reverse order.
Note:
1) [:] And =
Both of them can be assigned values, but = is to reference the assigned value. The obtained list points to the original content, modifying one list and the other changes.
[:] Is a value assignment. modifying a new or original list does not affect another list.
4. Others
List. count (obj)
Count the number of times an element appears in the list
List. index (obj)
Locate the index location of the first matching item of a value from the list
List. reverse ()
Elements in the reverse list
+: Directly combine the two lists into one.