List objects support additional operations that allow in-place modification of the object. Other mutable sequence types (when added to the language) should also support these operations. Strings and tuples are immutable sequence types: such objects cannot be modified once created. The following operations are defined on mutable sequence types (where x is an arbitrary object):
- s[y] = x : item y of s is replaced by x
- s[i:j] = t : slice of s from i to j is replaced by the contents of the iterable t
- del s[i:j] : same as "s[i:j] = []"
- s[i:j:k] = t : the elements of "s[i:j:k]" are replaced by those of t; note that t must have the same length as the slice being replaced.
- del s[i:j:k] : removes the elements of s[i:j:k] from the list
- s.append(x) : same as "s[len(s):len(s)] = [x]"
- s.extend(x) : same as "s[len(s):len(s)]" = x; x can be any iterable object
- s.count(x) : return number of i's for which s == x
- s.index(x[, i[, j]]) : return smallest k such that s[k] == x and i <= k < j; a ValueError is raised if x is not found in s. Negative values for the second ro third parameter to the index() method are converted by first adding the list length in order to get a positive number. If the result is still negative, the index is converted to zero.
- s.insert(i, x) : same as "s[i:i] = [x]"; a negative index results in the list length being added (as in slices). If the resulting index is still negative, zero is substituted.
- s.pop([y]) : same as "x = s[y]; del s[y]; return x"; this method is only supported by the list and array types. By default, the last item is removed and returned (i defaults to "-1".
- s.remove(x) : same as del s[s.index(x)]; a ValueError is raised if x is not found in s. Negative values for the second ro third parameter to the index() method are converted by first adding the list length in order to get a positive number. If the result is still negative, the index is converted to zero.
- s.reverse() : reverses the items of s in place. Note that, when manipulating a large list, sort() and reverse() modify the list in place for economy of space.
- s.sort([cmp[, key[, reverse]]]) : sort the items of s in place. Note that, when manipulating a large list, sort() and reverse() modify the list in place for economy of space.
Also, the sort() method takes optional arguments for controlling the comparisons. cmp specifies a custom comparison function of two arguments (list items) which return a negative, zero, or postive number depending respectively on whether the first argument is smaller than, equal to, or larger than the second argument. key is the function of one argument that is used to extrace a comparison key from each list element. reverse is a boolean value which indicates whether to reverse each comparison. The key and reverse processes are generally faster than specifying an equivalent cmp() function.
The sort() method will return stable results. That is, the relative order of elements that compare as equal will not be alterred.
