def unravel_index(index, shape):
    """ Analog of numpy.unravel_index. """
    out = []
    for dim in reversed(shape):
        out.append(index % dim)
        index = index // dim
    return tuple(reversed(out))