Preface: The definition of a list: A list consists of a series of elements arranged in a particular order. That is, the list is an ordered collection.
1. Modifying list elements
The preface is an ordered set, so we need to specify the list name and the index of the element to be modified when we modify the list element, and then specify the new value of the element.
For example, suppose you have a list of people, where the first person is called ' xiaohong ', how do you change his value?
names = ['xiaohong'Eric'Lily' ]print'xiaoming'print
In the above code, we first define a list of people, the first of which is called ' Xiaohong '. Next, we change the value of the first element to ' xiaoming '. The output shows that the value of the first element does change, but the value of the other list elements does not change:
[' Xiaohong ', ' Eric ', ' Lily ']
[' Xiaoming ', ' Eric ', ' Lily ']
You can modify the value of any element in a list, not just the value of the first element.
2. Adding elements to the list
2.1 Adding elements at the end of the list
Method Append () to add an element at the end of the list. Use the following:
names = ['xiaohong'Eric'Lily' ]print(names)# adds the element ' xiaoming ' to the end of the list names Names.append ( ' xiaoming ' )print(names)
After the run, the results are as follows:
[' Xiaohong ', ' Eric ', ' Lily ']
[' Xiaohong ', ' Eric ', ' Lily ', ' xiaoming ']
2.2 Inserting elements into a list
Use the Insert () method to add a new element anywhere in the list. To do this, you need to specify the index and value of the new element. As shown below:
names = ['xiaohong'Eric'Lily' ]print(names)# adds the element ' xiaoming ' to the list names " Xiaoming")print(names)
The run appears as follows:
[' Xiaohong ', ' Eric ', ' Lily ']
[' Xiaohong ', ' xiaoming ', ' Eric ', ' Lily ']
In the example above, the element "Xiaoming" is inserted into the second position of the list, the method insert () adds space at index 1, and the element "Xiaoming" is stored in this place. This action shifts all elements from the list to the right one position, starting at index 1.
2.2 Remove an element from the list
2.2.1 Deleting elements using the del statement
If you know the position of the element you want to delete in the list, the del statement is available.
Use the format as:
del list name [index value to delete element]
2.2.2 Using method Pop () to delete an element
The method pop () removes the element at the end of the list and allows you to continue to use it.
Use the format as:
List name. Pop ()
In fact, for us to use POP () to remove an element from any position in the list, simply specify the index value of the element to be deleted in parentheses.
Note: How do I choose to use the del statement or the pop () method? A simple criterion is: If you want to remove an element from the list and no longer use it in any way, use the del statement, or use the method pop () If you want to continue using it after you delete the element.
2.2.3 deleting elements based on values
Sometimes we don't know where the value to remove from the list is, but we know the value of the element to delete, so we can use the method remove ().
Use the format as:
List name. Remove (element value)
When you remove an element from a list by using remove () , you can then use its value, just like method Pop () .
Note: the Remove () method removes only the first specified value. If the value you want to delete appears more than once in the list, you need to use a loop to determine whether all such values have been deleted.
-------------------------------------------------------------------------------------Split Line--------------------------------- ---------------------------------------------------
PS: Blog Park Small Meng New, only to start soon, I hope you have a lot of advice!!
The purpose of editing this article is to record your own growth, and hope to provide some help to the latter.
Python3_ list (modify, add, and delete element actions)