First page Back Continue Last page Overview Graphics

Inserting and Deleting with Slicing

s[i:j] = t s[i:j] will be replaced by t

Example:

>>> s = [32,87,89,11,9]

>>> t = [333, 444, 555]

>>> s[1:3]

[87, 89]

>>> s[1:3] = t

>>> s

[32, 333, 444, 555, 11, 9]

s[i:j:k] = t the elements of

s[i:j:k]will be

replaced by t

Example:

>>> s = [32,87,89,11,9]

>>> t = [333, 444, 555]

>>> s[::2] = t

>>> s

[333, 87, 444, 11, 555]

del s[i] The element at

position i will be

removed

del s[i:j] The elements

s[i:j] will be

removed from s.

(equivalent to

s[i:j] = []).

del s[i:j:k] The elements of s[i:j:k]will be

removed from s.