append

As mentioned earlier, a list is an object whose content can be changed. The append method changes the contents of the list, adding an element after it

Such as

a = [1.2.3.14.'hello']
  
After # append, a becomes [1, 2, 3.14, 'hello', 'hello']
a.append('hello')
print(a)
 
# to append a becomes a [1, 2, 3.14, 'hello,' hello, [7, 8]]
a.append([7.8]])
print(a)
Copy the code

insert

The insert method can be used if, instead of adding an element later, we insert an element at the specified location

Such as

a = [1.2.3.14.'python3.vip']
 
Insert into index 0 at the position of the first element
The contents of the # a list become [' hello ', 1, 2, 3.14, 'python3. VIP ']
a.insert(0.'hello')
print(a)
 
Insert into index 2 and into the third element
# a list becomes [' hello ', 1, 'xiao Ming, 2, 3.14,' python3. VIP ']
a.insert(2.'Ming')
print(a)
Copy the code

pop

If we want to take and remove an element from the list, we can use the POP method. The argument to this method is the index of the element that you’re fetching and notice that once you’re fetching, that element is removed from the list. So pop is also often used to delete an element

Such as

#Python Learning Exchange group: 531509025
a = [1.2.3.14.'python3.vip']
 
Select the fourth element with index 3
poped = a.pop(3)
[1, 2, 3.14] [1, 2, 3.14]
print(a)
 
# The fetched element is assigned to the variable poped, which says 'python3. VIP '.
print(poped)
Copy the code

remove

The remove method also removes list elements.

The pop method takes the index of the element to be removed, and the remove method takes the value of the element to be removed.

Remove starts with the first element and looks for an element equal to the parameter object. If it finds one, remove it.

Once found, it does not continue to look for other equal elements.

That is, remove removes at most one element.

Such as

var1 = ['a'.'b'.'c'.'a']
var1.remove('a')
print(var1)
Copy the code

The result of running var1 becomes [‘ b ‘, ‘c’, ‘a’].

You can see that only the first element ‘a’ is deleted, and the last element ‘a’ is not deleted.

reverse

The Reverse method reverses the list elements

var1 = [1.2.3.4.5.6.7]
var1.reverse()
print(var1)
Copy the code

The result var1 becomes [7, 6, 5, 4, 3, 2, 1].