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!