Methods python list (change the original list)

  1. xxx.append () added to the end of an element

    renderings:

    Code:

    # The append () added to the end element 
    my_list = [ ' . 1 ' , ' 2 ' , ' . 3 ' , ' . 4 ' ]
     Print ( ' original list: ' , my_list) 
    my_list.append ( ' Five ' )
     Print ( ' now listing : ' , my_list)

     

  2. xxx.insert () to insert elements into the specified position
    renderings:

    Code:

    # INSERT () to insert elements into the specified position 
    my_list = [ ' . 1 ' , ' 2 ' , ' . 3 ' , ' . 4 ' ]
     Print ( ' before the specified insertion ' , my_list) 
    my_list.insert ( . 1, ' ha ' )
     Print ( ' specified after insertion ' , my_list)

     

  3. xxx.extend () to add a plurality of end
    renderings:

    Code:

    # Extend () to add a plurality of end 
    my_list = [ ' . 1 ' , ' 2 ' , ' . 3 ' , ' . 4 ' ]
     Print (my_list)
     # my_list.extend ([ 'fuel', 'ha']) feels more code below less 
    my_list + = [ ' fuel ' , ' ha ' ]
     Print (my_list)

     

  4. xxx.clear () Clear sequence
    renderings:

    Code:

    # Clear () Clear sequence 
    my_list = [ ' . 1 ' , ' 2 ' , ' . 3 ' , ' . 4 ' ]
     Print ( ' before emptying sequence: ' , my_list) 
    my_list.clear () 
    Print ( ' after emptying sequence: ' , my_list)

     

  5. xxx.pop () remove elements
    renderings:

    Code:

    # POP () remove and return the removed element based on the index 
    my_list = [ ' . 1 ' , ' 2 ' , ' . 3 ' , ' . 4 ' ]
     Print ( ' before the element deletion: ' , my_list) 
    deleted_elements = my_list.pop ( . 1 )
     Print ( ' after the element deletion: ' , my_list)
     Print ( ' elements deleted: ' , deleted_elements)

     

  6. xxx.remove () to delete the specified elements, if a plurality of the same elements, remove the first
    renderings:

    Code:

    # Remove () Removes the specified elements, if there are a plurality of the same elements, remove the first 
    my_list = [ ' . 1 ' , ' 2 ' , ' . 3 ' , ' . 4 ' , ' 2 ' ]
     Print ( ' element deletion of : before ' , my_list) 
    my_list.remove ( ' 2 ' )
     Print ( ' after the element deletion: ' , my_list)

     

  7. xxx.reverse ()   inverted list
    renderings:

    Code:

    # Reverse () inverted list 
    my_list = [ ' . 1 ' , ' 2 ' , ' . 3 ' , ' . 4 ' , ' . 5 ' ]
     Print ( ' pre inverted list: ' , my_list) 
    my_list.reverse () 
    Print ( ' after reversing the list: ' , my_list)

     

  8. xxx.sort () List Sort
    renderings:

    Code:

    # Sort () list is sorted by default ascending 
    # if desired in descending order, the need to pass a reverse = True as a parameter 
    my_list = [ ' . 1 ' , ' . 4 ' , ' . 3 ' , ' 2 ' , ' . 5 ' ]
     Print ( ' before ordering: ' , my_list) 
    my_list.sort () 
    Print ( ' after ascending: ' , my_list) 
    my_list.sort (Reverse = True)
     Print ( ' after descending: ' , my_list)

     

Guess you like

Origin www.cnblogs.com/FlyingLiao/p/11184173.html