def tensor(sizes=0, elem=0):
    """tensor(sizes=0, elem=0): creates a list of lists of lists...
    The parameter sizes can be a number or a tuple/list or sizes.

    >>> tensor()
    []
    >>> tensor(())
    []

    It works with a single number or a sequence of numbers:
    >>> tensor(3)
    [0, 0, 0]
    >>> tensor((3))
    [0, 0, 0]
    >>> tensor((3), None)
    [None, None, None]
    >>> tensor((2, 3)) # array of 2 rows and 3 colums.
    [[0, 0, 0], [0, 0, 0]]
    >>> tensor((2, 3), 2)
    [[2, 2, 2], [2, 2, 2]]
    >>> tensor((1, 2, 3))
    [[[0, 0, 0], [0, 0, 0]]]
    >>> tensor((2, 2, 3))
    [[[0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0]]]

    It works with mutables too, calling deepcopy:
    >>> r = tensor((2, 3), [3])
    >>> r
    [[[3], [3], [3]], [[3], [3], [3]]]
    >>> r[0][2][0] = 0
    >>> r
    [[[3], [3], [0]], [[3], [3], [3]]]
    """
    if isinstance(sizes, (int, long)):
        sizes = [sizes]
    elif not isinstance(sizes, list):
        sizes = list(sizes)

    result = []
    if sizes:
        first = sizes.pop()
        if isinstance(elem, (int, long, basestring, tuple, NoneType, bool)):
            result = [elem] * first
        else:
            result = [deepcopy(elem) for i in xrange(first)]
        while sizes:
            result = [deepcopy(result) for i in xrange(sizes.pop())]
    return result
tensor(sizes=0, elem=0): creates a list of lists of lists...
    The parameter sizes can be a number or a tuple/list or sizes.

    >>> tensor()
    []
    >>> tensor(())
    []

    It works with a single number or a sequence of numbers:
    >>> tensor(3)
    [0, 0, 0]
    >>> tensor((3))
    [0, 0, 0]
    >>> tensor((3), None)
    [None, None, None]
    >>> tensor((2, 3)) # array of 2 rows and 3 colums.
    [[0, 0, 0], [0, 0, 0]]
    >>> tensor((2, 3), 2)
    [[2, 2, 2], [2, 2, 2]]
    >>> tensor((1, 2, 3))
    [[[0, 0, 0], [0, 0, 0]]]
    >>> tensor((2, 2, 3))
    [[[0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0]]]

    It works with mutables too, calling deepcopy:
    >>> r = tensor((2, 3), [3])
    >>> r
    [[[3], [3], [3]], [[3], [3], [3]]]
    >>> r[0][2][0] = 0
    >>> r
    [[[3], [3], [0]], [[3], [3], [3]]]
    
recipes/Python/498123_tensor_and_transpose/recipe-498123.py