from functools import reduce
def validate(array, sizes):
    if reduce(mul, sizes) != len(array):
        raise ValueError('Array dimension incompatible with desired sizes')
return array, sizes

def reshape(array, sizes):
    for s in sizes:
        array = [array[i:i + s] for i in range(0, len(array), s)]
return array[0]
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
length = [2, 2, 3]
print(reshape(validate(data, length)))
length = [2, 2, 2]
print(reshape(validate(data, length)))
Python list to XML and vice versa