List objects support additional operations that allow in-place modification of the object. These operations would be supported by other mutable sequence types (when added to the language) as well. Strings and tuples are immutable sequence types and such objects cannot be modified once created. The following operations are defined on mutable sequence types (where x is an arbitrary object):
Operation | Result | Notes |
---|---|---|
s[i] = x |
item i of s is replaced by x | |
s[i:j] = t |
slice of s from i to j is replaced by t | |
del s[i:j] |
same as s[i:j] = [] |
|
s.append(x) |
same as s[len(s):len(s)] = [x] |
(1) |
s.extend(x) |
same as s[len(s):len(s)] = x |
(2) |
s.count(x) |
return number of i's for which s[i] == x |
|
s.index(x) |
return smallest i such that s[i] == x |
(3) |
s.insert(i, x) |
same as s[i:i] = [x]
if i >= 0 |
(4) |
s.pop([i]) |
same as x = s[i]; del s[i]; return x |
(5) |
s.remove(x) |
same as del s[s.index(x)] |
(3) |
s.reverse() |
reverses the items of s in place | (6) |
s.sort([cmpfunc]) |
sort the items of s in place | (6), (7) |
-1
,
so that by default the last item is removed and returned.
See About this document... for information on suggesting changes.