Search code examples
pythontypesimmutabilitymutable

Chart of mutable versus immutable types


Is there a table or a chart somewhere online which shows what types (inbuilt) are mutable and immutable in python?


Solution

  • 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