List (three)

List method

lst.append()
(add an element to the end of the list )
lst.append(parameter)
Insert picture description here

Insert picture description here

lst.insert()
(insert an element at the specified position of the list)
lst.insert (parameter 1, parameter 2)
parameter 1: the position to be inserted
parameter 2: the element to be inserted
Insert picture description here

Insert picture description here

lst.extend()
(Extend the current sequence, it needs a sequence as a parameter, it will add the parameters in the sequence to the current sequence)
Insert picture description here

Insert picture description here

lst.pop()
deletes the element according to the index value and returns the deleted element (with return value)
(if there is no index value, the last value is returned by default)
Insert picture description here

Insert picture description here

lst.remove()
deletes elements according to the specified element, no return value
Insert picture description here
Insert picture description here

lst.clear()
clears the list
Insert picture description here
Insert picture description here

lst.reverse() to
reverse the list
Insert picture description here
Insert picture description here

lst.sort()
sorts the elements in the list, the default ascending order
Insert picture description here

Insert picture description here
Descending order can be performed by reverse
Insert picture description here

Insert picture description here

Traverse the list

Refers to extracting the elements in the list

for loop : the code in the for loop will be executed multiple times, and it will execute as many times as there are elements in the sequence
Insert picture description here

Insert picture description here
range() function :
format: range(start, end, step)
Insert picture description here

Insert picture description here
Start: Default is 0 (starting from 0)
End: Excluding end (close before and open)
Step size: Default is 1

Guess you like

Origin blog.csdn.net/weixin_51864831/article/details/109552493