How to test for type or instance of dict_values?

Thorsten Kampe thorsten at thorstenkampe.de
Thu Nov 17 09:57:25 EST 2016


* Peter Otten (Thu, 17 Nov 2016 13:38:26 +0100)
> 
> Thorsten Kampe wrote:
> 
> > How can I test for type or instance of dictviews like dict_values?
> 
> Why do you want to?

Thanks, for the `collections.abc.ValuesView` tip.

The code in question is part of an attempt to get the dimensions of 
multi-dimensional lists, the `isinstance` is there in order to 
exclude strings.

"""
def dim(seq):
    dimension = []
    while isinstance(seq, (list, tuple)):
        dimension.append(len(seq))
        try:
            seq = seq[0]
        except IndexError:  # sequence is empty
            break
    return dimension
"""

Thorsten




More information about the Python-list mailing list