Instances of all methods in the list type (except sort) and explanations (called through help (list))

1. Overview

After using help (list) and running it, you can see the following methods: append, clear, copy, count, extend, index, insert, pop, remove, reverse, a total of ten methods. Next, I will give examples one by one. explain.

2. Examples and explanations

1.append: Append object to the end of the list. (Append object to the end of the list.) This object can be any element, because the list is a container, and the container is A data type that can store multiple elements

Here we append a list object to the end of another list:

data = [1, 2]
data_list = [3]
data.append(data_list)
print(data, type(data))

The running results are as follows:

You can see that a list has been successfully appended to the end of the original list.

2.clear: Remove all items from list.

my_list = [1, 2, 3, 4, 5]
print(my_list)
my_list.clear()
print(my_list)

The running results are as follows:

It can be seen that all elements have been successfully deleted from the list

3.copy: In Python, my_list.copy is used to create a shallow copy of a list. Shallow copy means creating a new list whose elements also point to objects in the original list. If the elements in the list are mutable objects (such as lists), modifying the elements in the new list will also affect the original list

For example, if we create a new list, the sixth element is a list

my_list = [1, 2, 3, 4, 5, [6, 7]]

Print and output the first element in element number 6, and the entire list

print(my_list[5][0], my_list)

Create another shallow copy and use a variable copied_list to receive it

copied_list = my_list.copy()

Modify the value of the first element in the sixth element in the shallow copy list

copied_list[5][0] = 8

Finally, print the original list and the shallow copy list respectively, and you will find that the first value of the sixth element in both lists has been changed.

print(my_list, copied_list)

The complete code is as follows:

my_list = [1, 2, 3, 4, 5, [6, 7]]
print(my_list[5][0], my_list)
copied_list = my_list.copy()
copied_list[5][0] = 8
print(my_list, copied_list)

4.count: Return number of occurrences of value.

We define a list, enter three 1’s, and use the count method to see if we can return the number of occurrences of 1.

code show as below:

my_list = [1, 2, 1, 1]
print(my_list.count(1))

The results are as follows, indicating that there is no problem with the code

5. extend: Extend the list by appending elements from the iterable. Iterable is an iterable object in Python.

my_list1 = [1, 2, 3]
my_list2 = [4, 5, 6]
my_list1.extend(my_list2)
print(my_list1)

The running results are as follows:

6.index: Return the first index of value (return the first index of value)

If you want to simply return the first value

code show as below:

my_tuple = (1, 2, 3)
data = my_tuple.index(1)
print(data)

Because it is the index of the first value, it is 0

If we get an unknown tuple and want to access the second 1 in it, we can first use the count method to print out how many 1s there are, then use the index method to access the index of the first 1, and finally use the index method Access the second index

my_list = [1, 2, 3, 1, 3, 1]
data1 = my_list.count(1)
print(data1)
data2 = my_list.index(1)
print(data2)
data3 = my_list.index(1) + 1
print(my_list.index(1, data3))

7.insert: Insert object before index (Insert object before index.)

It should be noted that in the insert method, we need to first enter the index before which the object is to be inserted, and then enter the object to be inserted.

Take the next line of code we want to output as an example:

data_tuple[4].insert(1, 8)

We define a tuple tuple, and we define the fourth element of the tuple tuple as a list. We try to insert an element into the list using the insert method:

data_tuple = (1, 2, 3, 4, [5, 6])
data_tuple[4].insert(1, 8)
print(data_tuple)

The output is as follows:

8.pop: Remove and return item at index (Remove and return item at index)

When using the pop method, we can delete the variable type inside the list or tuple.

Still the tuple in the insert method, we try to use the pop method to delete the 6 elements in the fourth tuple (index value 1)

data_tuple = (1, 2, 3, 4, [5, 6])
data_tuple[4].pop(1)
print(data_tuple)

The running results are as follows:

9.remove: Remove first occurrence of value.

We define a list with as many 1s appearing in it as possible, and then use the remove method to check the effect. Similarly, if this is an unknown list, we can use the count method to count the number of 1s that appear.

my_list = [1, 2, 3, 1, 2, 1]
data = my_list.count(1)
print(data)
my_list.remove(1)
print(my_list)

The running results are as follows:

10.reverse: Reverse *IN PLACE* in the reverse method means to reverse the list. When we call list.reverse(), the list will be reversed

code show as below:

my_list = [1, 2, 3, 4, 5]
my_list.reverse()
print(my_list)

The result is as follows:

3. Summary

List in Python is a data type, and there are many methods for manipulating lists. The following is a brief summary of some commonly used methods in lists:

Here are some common methods for list types in Python. Here is a brief summary of these methods:

append(self, object, /): Appends the object to the end of the list.
clear(self, /): Remove all elements from the list.
copy(self, /): Returns a shallow copy of the list.
count(self, value, /): Returns the number of occurrences of the value in the list.
extend(self, iterable, /): Extend the list by appending the elements in the iterator.
index(self, value, start=0, stop=9223372036854775807, /): Returns the index of the first occurrence of the value in the list. If the value does not exist, ValueError is raised.
insert(self, index, object, /): Insert an object before the given index.
pop(self, index=-1, /): Removes and returns the element at the specified index (default is the last element). If the list is empty or the index is out of range, IndexError is raised.
remove(self, value, /): Remove the first occurrence of the value in the list. If the value does not exist, ValueError is raised.
reverse(self, /): Reverse the list in place

The above are some commonly used ten methods of Python list. Mastering these methods can help you better process list data.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry-level skills treeBasic grammarBuilt-in classes 389131 people are learning the system