This is a summary of container-like types, their characteristics and available operations.
Sequence | Mutable | Limitation on elements | |
---|---|---|---|
str |
✔ | Unicode characters | |
tuple |
✔ | ||
bytes |
✔ | bytes | |
list |
✔ | ✔ | |
bytearray |
✔ | ✔ | bytes |
frozenset |
|||
set |
✔ | immutable, hashable | |
dict |
✔ | key must be hashable |
In the following table, "obj" is an object of the given type, "x" is some object, "i", "j", "k" and "n" are non-negative integers, and "t" is an iterable object. "*t" indicates one or more iterables.
tuple |
bytes |
list |
bytearray |
frozenset |
set |
dict |
|
---|---|---|---|---|---|---|---|
Constructor: | |||||||
class( iterable) |
✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
Accessing elements: | |||||||
obj[ i] |
✔ | ✔ | ✔ | ✔ | |||
obj[ i: j] |
✔ | ✔ | ✔ | ✔ | |||
obj[ i: j: k] |
✔ | ✔ | ✔ | ✔ | |||
obj[ key] |
✔ | ||||||
Membership: | |||||||
x in obj |
✔ | ✔ | ✔ | ✔ | ✔ | ✔ | |
x not in obj |
✔ | ✔ | ✔ | ✔ | ✔ | ✔ | |
key in obj |
✔ | ||||||
obj.index( x) |
✔ | ✔ | ✔ | ✔ | |||
obj.index( x, i) |
✔ | ✔ | ✔ | ✔ | |||
obj.index( x, i, j) |
✔ | ✔ | ✔ | ✔ | |||
obj.count( x) |
✔ | ✔ | ✔ | ✔ | |||
Assigning to elements: | |||||||
obj[ i] = x |
✔ | ✔ | |||||
obj[ i: j] = t |
✔ | ✔ | |||||
obj[ i: j: k] = t |
✔ | ✔ | |||||
obj[ key] = x |
✔ | ||||||
Adding elements: | |||||||
obj.append( x) |
✔ | ✔ | |||||
obj.extend( t) |
✔ | ✔ | |||||
obj.insert( i, x) |
✔ | ✔ | |||||
obj += t |
✔ | ✔ | |||||
obj *= n |
✔ | ✔ | |||||
obj.add( x) |
✔ | ||||||
obj.update( *t) |
✔ | ||||||
Removing elements: | |||||||
del obj[ i] |
✔ | ✔ | |||||
del obj[ i: j] |
✔ | ✔ | |||||
del obj[ i: j: k] |
✔ | ✔ | |||||
obj.pop() |
✔ | ✔ | ✔ | ||||
obj.pop( i) |
✔ | ✔ | |||||
obj.remove( x) |
✔ | ✔ | ✔ | ||||
obj.discard( x) |
✔ | ||||||
obj.clear() |
✔ | ✔ | ✔ | ||||
Other operations: | |||||||
obj.copy() |
✔ | ✔ | ✔ | ||||
obj1 + obj2 |
✔ | ✔ | ✔ | ✔ | |||
obj * n n * obj |
✔ | ✔ | ✔ | ✔ | |||
len(obj) |
✔ | ✔ | ✔ | ✔ | |||
max(obj) |
✔ | ✔ | ✔ | ✔ | |||
min(obj) |
✔ | ✔ | ✔ | ✔ | |||
obj.reverse() |
✔ | ✔ | |||||
obj.sort( *, key=None, reverse=False) |
✔ |
Sets have several additional set-specific methods...
Strings have many additional string-specific methods...