append()
append() Method : Add elements at the end of the list
# Append an element to the end of the list
my_list = ['a', 'b', 'c']
my_list.append('d')
print(my_list)
# Append the entire list to the end of the list
my_list2 = ['d', 'e', 'f']
my_list.append(my_list2)
print(my_list)
Execution results :
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd', ['d', 'e', 'f']]
insert()
insert() Method : Add an element at the specified index position of the list
my_list3 = ['a','b','d','e']
my_list3.insert(2,'c')
print(my_list3)
Execution results :
['a', 'b', 'c', 'd', 'e']
extend()
extend() Method : Add the elements in a list from the beginning to the list one by one
my_list3 = ['a', 'b', 'c', 'd', 'e']
my_list4 = ['f','g','h']
my_list3.extend(my_list4)
print(my_list4)
Execution results :
['a', 'b', 'c', 'd', 'e','f', 'g', 'h', ]
del Delete elements according to subscript
del keyword : Delete according to the target element index
my_list7 = [41, 63, 89, 22, 37, 101, 77, 56, 16, 19]
# Delete single element
del my_list7[3]
print(my_list7)
# Delete the element of a section
del my_list7[2:4]
print(my_list7)
Execution results :
[41, 63, 89, 37, 101, 77, 56, 16, 19]
[41, 63, 101, 77, 56, 16, 19]
When used directly del When receiving list name , Will delete the entire list
pop() Delete the last element of the list
pop() Method : When subscript is not specified , By default, the last element in the list will be deleted ( Similar to the stack out operation ), Specifying the subscript will delete the specified element
my_list7 = [41, 63, 89, 22, 37, 101, 77, 56, 16, 19]
my_list7.pop()
print(my_list7)
Execution results :
[41, 63, 89, 22, 37, 101, 77, 56, 16]
remove() Delete... Based on element value
remove() Method : Delete according to the value of the element , If you have two values , Only the first , If the value does not exist, an error is reported
my_list7 = [41, 63, 89, 22, 37, 101, 77, 56, 16, 19, 101]
my_list7.remove(63)
my_list7.remove(101)
print(my_list7)
Execution results :
[41, 89, 22, 37, 77, 56, 16, 19, 101]
clear() Delete all elements
clear() Method : Delete all elements in the list
my_list7 = [41, 63, 89, 22, 37, 101, 77, 56, 16, 19, 101]
my_list7.clear();
print(my_list7)
Execution results : The list is cleared
[]
Modify individual elements
Reassign an element , Is to modify this element :
my_list5 = [41, 63, 89, 22, 37, 101, 77, 56]
my_list5[2] = -36
my_list5[-3] = -202 # Negative subscripts start at the far right , by -1
print(my_list5)
Execution results :
[41, 63, -36, 22, 37, -202, 77, 56]
Modify a set of elements
Specify an interval ( Left closed right away ), Re assign values to the elements in this interval
my_list5 = [41, 63, 89, 22, 37, 101, 77, 56]
my_list5[2:4] = [98,33,55]
print(my_list5)
Execution results : The specified interval is [2-4), The interval has only two elements , But we assign three values , The extra value will be added to the list
[41, 63, 98, 33, 55, 37, 101, 77, 56]
But if the step size is specified in the interval , It requires the same number of interval elements and assignments , Otherwise, an error will be reported , Amendment No 1,3,5 Elements :
my_list5 = [41, 63, 89, 22, 37, 101, 77, 56, 16, 19]
my_list5[1:7:2] = [36, 33, 202]
print(my_list5)
Execution results :
[41, 36, 89, 33, 37, 202, 77, 56, 16, 19]
in and not in
in: If the element exists in the list , The result is true, Instead of false
not in: If the element does not exist in the list , The result is true, Instead of false
hero_list = [" Berserker iron "," Li Bai "," Sun Bin "]
hero = input(" Please select a hero :")
if hero in hero_list:
print(" Common Heroes ")
else:
print(" Not in the hero pool ")
Execution results :
Please select a hero : Li Bai
Common Heroes
Please select a hero : concubine
Not in the hero pool
index()
index() Method : Returns the index subscript of the first occurrence of an element in the list , An error is reported if the element does not exist
my_list6 = [41, 63, 89, 22, 37, 101, 77, 56, 16, 19,101]
print(my_list6.index(101))
# Find in the specified interval ( Left closed right open interval )
print(my_list6.index(37,2,7))
Execution results :
5
4
count()
count() Method : Returns the number of times an element appears in the list
my_list6 = [41, 63, 16, 22, 37, 101, 16, 56, 16, 19,101]
print(my_list6.count(16))
print(my_list6.count(1))
Execution results :
3
0