List Operations

The following operations are applicable only to lists

     s[i] = x           Element assignment
     s[i:j] = r         Slice assignment
     del s[i]           Element deletion
     del s[i:j]         Slice deletion

Examples

     a = [0,1,2,3,4,5,6]
     a[3] = -10              # a = [0,1,2,-10,4,5,6]
     a[1:4] = [20,30,40,50]  # a = [0,20,30,40,50,4,5,6]
     a[1:5] = [100]          # a = [0,100,4,5,6]
     del a[1]                # a = [0,4,5,6]
     del a[2:]               # a = [0,4]

Note:

  • Strings and tuples are immutable and can't be modified.
<<< O'Reilly OSCON 2000, Introduction to Python, Slide 57
July 17, 2000, beazley@cs.uchicago.edu
>>>