[Python] Introduction to common methods of lists


foreword

This article mainly briefly introduces the common methods of lists in python, including adding elements, clearing lists, deleting elements, copying lists, inserting elements according to subscripts, deleting elements according to subscripts, flipping lists, and sorting lists.


1. Adding elements to the list

Use the append() method to add elements to the list, and the new elements are appended to the end of the list. The code example is as follows:

oldstr = [1,2,3]
print(oldstr)
oldstr.append('4')
print(oldstr)

Execute this script, you can see that a new element has been added after the original list, and the result is as follows:
Append an element to the end of the list

2. Clear the list

Use the clear() method to clear all the elements in the list. The list is not deleted, but the elements inside are cleared. The code example is as follows:

oldstr = [1,2,3]
print(oldstr)
oldstr.clear()
print(oldstr)

After executing this piece of code, all elements in the list will be cleared, and the running results are as follows:
clear the list

3. Delete the specified element of the list

Use the remove() method to delete the specified element in the list, and fill in the element value in the method. The code example is as follows:

oldstr = [1,2,3]
print(oldstr)
oldstr.remove(3)
print(oldstr)

After executing this piece of code, the '3' element of the list will be deleted, and the running results are as follows:
Delete the specified element of the list

4. Copy list

Use the copy() method to copy the list and copy all the contents of the list to the new list. The code example is as follows:

oldstr = [1,2,3]
print('oldstr =',oldstr)
newstr = oldstr.copy()
print('newstr =',newstr)

After executing this piece of code, copy all the contents of the old list to the new list, and the running results are as follows:
copy list

5. Insert data to the specified position in the list according to the subscript

Use the insert(i, value) method to add data to the specified position in the list, where i is the subscript starting from 0, and value is the inserted element value. If insert(0,6) is used, 6 is inserted at the first position, and if insert(1,6) is used, 6 is inserted at the second position. Create two lists here, and insert elements to the specified position to demonstrate the use of the Insert() method. The code example is as follows:

#在第一个位置插入
oldstr = [1,2,3]
print(oldstr)
oldstr.insert(0,6)
print(oldstr)
#在第二个位置插入
oldstr1 = [1,2,3]
print(oldstr1)
oldstr1.insert(1,6)
print(oldstr1)

After executing this piece of code, you can see that elements are inserted into the specified positions in the two lists, and the code execution results are as follows:
Insert data into the specified position of the list according to the subscript

6. Delete elements according to their subscripts

Use the pop(i) method to delete the element at the specified position in the list, i is the subscript of the element, starting from 0 from the beginning to the end, and starting from -1 from the end to the beginning. The code example is as follows:

oldstr = [1,2,3]
print(oldstr)
oldstr.pop(0)
print(oldstr)

After executing this piece of code, the first position element 1 will be deleted, and the result of code execution is as follows:
Delete elements based on their subscripts

7. Flip list elements

Use the reverse() method to reverse the order of the list elements. If the original list is [1,2,3], then the reversed list is [3,2,1]. The code example is as follows:

oldstr = [1,2,3]
print(oldstr)
oldstr.reverse()
print(oldstr)

After executing this piece of code, you can see that the order of the list elements has been reversed, and the result of code execution is as follows:
list element flipping

8. Sorting list elements

Use the sort() method to sort the elements in the list. If it is a number, it will be sorted from small to large. If it is a letter, it will be sorted alphabetically. The code example is as follows:

oldstr = [4,2,7,6,9,2,4,1]
print(oldstr)
oldstr.sort()
print(oldstr)

After executing this code, you can see that the elements in the list are sorted from small to large, and the code execution results are as follows:
Sort list elements


Summarize

This article mainly briefly introduces the common methods of lists in python, including adding elements, clearing lists, deleting elements, copying lists, inserting elements according to subscripts, deleting elements according to subscripts, flipping lists, and sorting lists. This article is only for learning summary, if there is something wrong, please correct me.

Supongo que te gusta

Origin blog.csdn.net/liaotianyin/article/details/130709847
Recomendado
Clasificación