List slice assignment: delete and insert : List Sliced « List « Python






List slice assignment: delete and insert

List slice assignment: delete and insert
 
L = ['spam', 'Spam', 'SPAM!']

print L

L[0:2] = ['eat', 'more']       # slice assignment: delete+insert
print L                        # replaces items 0,1



           
         
  








Related examples in the same category

1.List slicingList slicing
2.List: display a sliceList: display a slice
3.List indices start at 0, and lists can be sliced, concatenated:List indices start at 0, and lists can be sliced, concatenated:
4.Getting a List Subset: SlicingGetting a List Subset: Slicing
5.Slicing works similar to strings; use the square bracket slice operator ([ ] ) along with the index or indices.
6.List Slicing ShorthandList Slicing Shorthand