Mutable Sequence Types
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 can’t be modified
once created. The operations in the following table are defined on
mutable sequence types (where x
is an arbitrary
object).
Operation |
Result |
Notes |
|
Item | |
|
Slice of | |
|
Same as | |
|
Same as | |
|
Same as |
5 |
|
Return number of | |
|
Return smallest |
1 |
|
Same as | |
|
Same as |
4 |
|
Same as |
1 |
|
Reverses the items of |
3 |
|
Sort the items of |
2, 3 |
Notes
This raises an exception when
x
is not found ins
.The
sort()
method takes an optional argument specifying a comparison function of two arguments (list items) that should return-1
,0
, or1
depending on whether the first argument is considered smaller than, equal to, or larger than the second argument. Note that this slows the sorting process considerably; e.g., to sort a list in reverse order, it’s much faster to use calls to ...
Get Python Programming On Win32 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.