Add, delete, modify, and query instance code for the python list.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.