import numpy as np
from itertools import product

def iterslice(data_shape, width):
    assert (all(sh >= width for sh in data_shape),
        'all axes lengths must be at least equal to width')
start_indices = [list(range(sh - width + 1)) for sh in data_shape]
start_coords = product(*start_indices)
for start_coord in start_coords:
    yield tuple(slice(coord, coord + width) for coord in start_coord)
arr = np.arange(0, 5 ** 3).reshape(5, 5, 5)
data_slices = iterslice(arr.shape, 3)
sub_arrays = [arr[ds] for ds in data_slices]
Extract hyper-cubical blocks from a numpy array with unknown number of dimensions