Add, delete, modify, and query instance code for the python list.
Names = ["zhao00", "qian01", "sun02", "li03", "li03", "li03", "zhou04"] # print (names []) print error print (names)
Add
Names. append ("wu05") # add to the last names. insert (1, "zheng06") # add to the specified position
Change
Names [0] = "zhao" # modify
Delete
# Names. remove ("zheng06") # del names [5] # names. pop () # No subscript. The last # names is deleted by default. pop (0) # print (names) # del names deletes the entire list # names. clear () deletes the list elements
Query
Print (names. index ("qian01") # query position print (names [names. index ("qian01")]) # print the data print (names. count ("li03") # count the number of identical elements
Merge
Names2 = ["01", "02", "03"] names. extend (names2) # merge
Special output
Print (names [0: 3]) # Left closed and right open, equivalent to [: 3] print (names [-1]) # obtain the last print (names [-2:]) # Take the last two, [-], [-2:-1]
Output of the entire list
. Print (names )----------. for I in names: print (I )----------. print (names [0:-]) # interval an output print (names [0:-])
The code for adding, deleting, modifying, and querying instances in the above python list is all the content that I have shared with you. I hope to give you a reference and support for the help house.