When using a list, you need to use many methods, such as traversing the list, finding elements, adding elements, deleting elements, changing elements, inserting elements, sorting the list, reversing the list, etc.
Some operations will be completed through corresponding functions. The functions are introduced in the following table:
Traversing a list is usually implemented using a for loop or a combination of a for loop and the enumerate() function.
This method is relatively simple. It has been used many times when we talked about the for loop before. It is directly used for traversal and loop execution. Take a look at the code.
first_list=[1,2,3,4]#First define a list foriinfirst_list:#i is used to save the element values obtained from the list. When you want to output the element, just output i directly. print(i)
Output result:
1234
The enumerate function was mentioned once in the sequence. Its function is to combine the sequence into an index sequence. When we use it with a for loop, we can get the elements in the list and their index values.
The syntax format is:
forindex,elementinenumerate(list):
The index value is the index value, element refers to the element, and list value is the list we want to traverse. Let’s look at an example.
my_list=['Xiao Ming','Xiao Hua','Xiao Tian','Xiao Na','Xiao Mei','Xiao Li']forindex,elementinenumerate(my_list):print('The serial number is:',index,' The name is: ',element)
The output is:
The serial number is: 0 The first name is: Xiaoming The serial number is: 1 The first name is: Xiaohua The serial number is: 2 The first name is: Xiaotian The serial number is: 3 The first name is: Xiaona The serial number is: 4 The first name is: Xiaomei The serial number is: 5 The first name is :Xiao Li
This way we can more clearly see the position of each element in the list.
When searching for an element, we can use the index() method, which will return the index value of the element. If the element cannot be found, an error will be reported.
my_list=['Xiao Ming','Xiao Hua','Xiao Tian','Xiao Na','Xiao Mei','Xiao Li']print(my_list.index('Xiao Tian'))
Output result:
2
Adding elements is relatively simple, use the append() method to add.
my_list=[]#Create an empty list my_list.append(1)#Add an element 1my_list.append(2)#Add an element 2print(my_list)#Output
The output is:
[1,2]
When deleting elements, we usually use two methods, namely deleting based on index value and deleting based on element value.
my_list=['Xiao Ming','Xiao Hua','Xiao Tian','Xiao Na','Xiao Mei','Xiao Li']delmy_list[1]#Delete the element with index value 1, corresponding to'Xiaohua 'print(my_list)
Output result:
['Xiao Ming','Xiao Tian','Xiao Na','Xiao Mei','Xiao Li']
When deleting based on element value, we will use the remove() function to delete.
The code is as follows:
my_list=['Xiao Ming','Xiao Hua','Xiao Tian','Xiao Na','Xiao Mei','Xiao Li','Xiao Tian']my_list.remove('Xiao Tian')#Directly find the first An element for '小天' print(my_list)
Output result:
['Xiao Ming','Xiao Hua','Xiao Na','Xiao Mei','Xiao Li','Xiao Tian']
We can find that it only deletes the first element named 'Xiaotian'. If you want to delete them all, you can delete them with if statements, etc. You can practice it yourself.
If we want to change the value of an element in the list, we can change it directly, for example:
my_list=['Xiao Ming','Xiao Hua','Xiao Na','Xiao Mei','Xiao Li','Xiao Tian']my_list[0]='Xiao Ming's brother'print(my_list)
Output:
['Xiao Ming's brother','Xiao Hua','Xiao Na','Xiao Mei','Xiao Li','Xiao Tian']
When we want to add an element to a certain position in the list, we can use the insert(index,element) method, where index is the index position and element is the inserted element.
When elements are inserted into a list, the list size expands to accommodate the new elements. The element previously at the specified index position and all elements after it are moved backward one position in turn.
If you specify an invalid index, no exception is raised.
If the specified position is beyond the end of the list, the element is added to the end of the list.
If you specify an illegal index using a negative index, the index will be added to the beginning of the list.
Let’s look at an example:
It is known that a list is: ['Xiao Ming', 'Xiao Hua', 'Xiao Na', 'Xiao Mei', 'Xiao Li', 'Xiao Tian'], we need to insert an element between Xiao Ming and Xiao Hua' Xiao Zhang', then the first thing we need to find is that Xiaohua's index value is 1, then we can use the insert method to insert directly.
The code is as follows:
my_list=['Xiao Ming','Xiao Hua','Xiao Na','Xiao Mei','Xiao Li','Xiao Tian']my_list.insert(1,'Xiao Zhang')print(my_list)
The output is:
['Xiao Ming','Xiao Zhang','Xiao Hua','Xiao Na','Xiao Mei','Xiao Li','Xiao Tian']
In the next section we will learn about sorting and reversing lists.