def shape(lst):
    def ishape(lst):
        shapes = [ishape(x) if isinstance(x, list) else [] for x in lst]
        shape = shapes[0]
        if shapes.count(shape) != len(lst):
            raise ValueError('Ragged list')
        shape.append(len(lst))
        return shape
    return tuple(reversed(ishape(lst)))

print(shape([1, 2, 3, 4, 5, 6]))
print(shape([[1, 2, 3], [4, 5, 6]]))
print(shape([[1, 2], [3, 4], [5, 6]]))
print(shape([[[1, 2, 3, 4, 5, 6]]]))
print(shape([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]))
print(shape([[1, 2, 3], [4, 5]]))