Is there a table or a chart somewhere online which shows what types (inbuilt) are mutable and immutable in python?
An object is mutable if it is not immutable. An object is immutable if it consists, recursively, of only immutable-typed sub-objects. Thus, a tuple of lists is mutable; you cannot replace the elements of the tuple, but you can modify them through the list interface, changing the overall data. Save this answer.
I am not sure of a chart, but basically:
Mutable:
list
, dictionary
, bytearray
Note: bytearray
is not a sequence though.
Immutable:
tuple
, str
You can check for mutability with:
>>> import collections
>>> l = range(10)
>>> s = "Hello World"
>>> isinstance(l, collections.MutableSequence)
True
>>> isinstance(s, collections.MutableSequence)
False
For a dictionary (mapping):
>>> isinstance({}, collections.MutableMapping)
True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With