Operation |
Grammar |
Example |
Results |
modifying elements |
|
Motocycles = [' Honda ', ' Yamaha ', ' Suzuki '] Print (Motocycles) Motocycles[0] = ' Ducati ' Print (Motocycles) |
[' Honda ', ' Yamaha ', ' Suzuki '] [' Ducati ', ' Yamaha ', ' Suzuki '] |
add element |
Add an element at the end of the list: Append () |
motocycles = [' Honda ', ' Yamaha ', ' Suzuki '] print (motocycles) motocycles.append (' Ducati ') print (motocycles) |
[' Honda ', ' Yamaha ', ' Suzuki ']   [' Honda ', ' Yamaha ', ' Suzuki ', ' Ducati ' ] |
|
motocycles = [' Honda ', ' Yamaha ', ' Suzuki '] print (motocycles) motocycels.insert (0, ' Ducati ') Print (motocycles) |
[' Honda ', ' Yamaha ', ' Suzuki '] [ ' Ducati ' , ' Honda ', ' Yamaha ', ' Suzuki '] |
delete element |
Delete by index: Del, unable to continue using |
motocycles = [' Honda ', ' Yamaha ', ' Suzuki '] Print (motocycles) del motocycles[1] print (motocycles) |
[' Honda ', ' Yamaha ', ' Suzuki '] [' Honda ', ' Suzuki '] |
Delete the element at the end: Pop (), you can continue using can also be deleted according to the index, such as Motocycles.pop (0) |
Motocycles = [' Honda ', ' Yamaha ', ' Suzuki '] Print (motocycles) popped_motocycle = Motocycles.pop () print (motocycles) print (popped_motocycle) |
[' Honda ', ' Yamaha ', ' Suzuki '] [' Honda ', ' Yamaha '] suzuki |
when you do not know the index, delete the element by value: Remove (), you can continue using |
motocycles = [' Honda ', ' Yamaha ', ' Suzuki ', ' Ducati '] Print (motocycles) too_expensive = ' Ducati ' motocycles.remove (too_expensive) print (motocycles) print (too_expensive) |
[' Honda ', ' Yamaha ', ' Suzuki ', ' Ducati '] [' Honda ', ' Yamaha ', ' Suzuki '] ducati |
element sort |
permanent sort: sort () Sort in reverse direction: sort (reverse = True) |
Cars = [' BMW ', ' Audi ', ' Toyota ', ' Subaru '] Print (Cars cars.sort () Print (cars) Span style= "COLOR: #000000" >cars = [' BMW ', ' Audi ', ' Toyota ', ' Subaru '] Cars.sort (reverse = True) Print (cars) |
[' BMW ', ' Audi ', ' Toyota ', ' Subaru '] [' Audi ', ' BMW ', ' Subaru ', ' Toyota '] [' Toyota ', ' Subaru ', ' BMW ', ' Audi '] |
temporary sort: sorted () Span style= "COLOR: #0000ff" > sort in reverse direction : sorted (reverse = True) |
Cars = [' BMW ', ' Audi ', ' Toyota ', ' Subaru '] Print (cars) print (sorted (cars)) print (Cars) |
[' BMW ', ' Audi ', ' Toyota ', ' Subaru '] [' Audi ', ' BMW ', ' Subaru ', ' Toyota '] [' BMW ', ' Audi ', ' Toyota ', ' Subaru '] |
Permanently reverse the order of list elements: reverse () |
Cars = [' BMW ', ' Audi ', ' Toyota ', ' Subaru '] Print (Cars) Cars.reverse () Print (Cars) Cars.reverse () Print (Cars) |
[' BMW ', ' Audi ', ' Toyota ', ' Subaru '] [' Subaru ', ' Toyota ', ' Audi ', ' BMW '] [' BMW ', ' Audi ', ' Toyota ', ' Subaru '] |