Full stack growth - data type of python study notes - list

Lists and Tuples

list operation
operation
+ [1,2,3]+[4,5,6]=[1,2,3,4,5,6]
* [1,2,3]*2=[1,2,3,1,2,3]
ps Both lists and tuples are available
Common methods for lists
method name usage
append(item) Add a new element at the end of the list (push method in js) [1].push(2)=[1,2]
insert(index,item) **index:** position index, **item:** element to be added Add an element to the specified position of the list
count(item) Count the number of occurrences of the item in the list item can be an element type or a dictionary type {"a": "123"} common to lists and tuples If the item does not exist in the list, return 0 , and the entire dictionary matches.
remove(item) Delete the item element in the list. If the item does not exist, an error will be reported directly. If there are multiple items, only the first one can be deleted, and the heartbeat data will not be returned. Only the original one will be deleted.
reverse() Reverse the original order of the list
sort(fun,key,reverse) fun list sorting method key parameter comparison reverse (default value is false) whether to reverse the element types need to be the same or an error will be reported
clear() No parameters, no return value, clearing the list without changing the address value is more efficient than directly assigning the list to []
copy() Make a deep copy of the current list and return an identical list
extend() list.extend(list2) Import list2 directly into list1 for a combined list and tuple
pop(index) Delete the element with index index and return this element to the original array and delete it directly

Guess you like

Origin blog.csdn.net/qq_51075057/article/details/130428595