Python3 list addition, deletion, modification, query, and merge sorting method

Source: Internet
Author: User
Tags pop value
This article describes in detail how to add, delete, modify, query, merge, and sort the python3 list. if you are interested, refer to # Auther: Aaron Fan.
Names = ["aaron", "alex", "james", "meihengfan"]
Names2 = [1, 2, 4, 5]
Print (names)
# Query
# Print (names) # List content
Print (names [3]) # 4th values in the access list
Print (names []) # values from 2nd to 3rd in the access list
Print (names [-1]) # The Last value in the access list
Print (names [:-2]) # All values in the access list, but remove the penultimate and subsequent values.
Print (names [-3:]) # The value from the last to the third in the access list
Print (names [0], names [3]) # note that when multiple values are obtained, you cannot directly write the subscript together. you need to write it in this way.
Print (names [: 2]) # print the list. However, if the step is 2, the chunk is skipped. you can change the step size as needed.
Print (names. index ("james") # search for the subscript of the element james in the list
Print (len (names) # determine the length of the list
# Add
Names. append ("jack") # insert an element at the end of the list
Names. insert (1, "fanheng") # insert fanheng to the second location
# Change
Names [2] = "liming" # Change the element at the third position to liming
# Delete
Names. remove ("liming") # Delete the liming element from the list
Del names [2] # Delete the third element. you must know the index of the element.
# Del names # directly delete the list
Names. pop () # Delete the element at the end of the list by default. of course, you can also directly specify the subscript of the element to pop up a specified element, and let you wait for it
# When you use pop, the pop-up elements are no longer in the list.
# Pop pops up an element from the list. The pop value can be directly assigned to other variables, for example:
Popend_name = names. pop ()
Print (popend_name)
# Names. clear () # clear the list. dangerous operations. use it with caution.
# Other operations
# Names. reverse () # reverse the list to completely reverse the original order.
# Sorting
# Names. sort () # permanently sort the list
Print (sorted (names) # sort the list temporarily
# Merge list
Names. extend (names2) # Merge names2 into names
Print (names)

The above is the details of the add, delete, modify, query, merge, and sort methods in the python3 list. For more information, see other related articles in the first PHP community!

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.