I have a python code that uses a numpy array to store multi-dimensional data. The dimensionality of the array is determined at runtime so I cannot know in advance the exact number of dimensions, which can be from 3 to 6. One thing I know, though, is that when a dimension exists in the array, it has 100 elements. So right now my analysis code looks something like this: ndim = myarray.ndim if ndim == 2: for p0 in range(100): do_something(myarray[:, p0]) if ndim == 3: for p0 in range(100): for p1 in range(100): do_something(myarray[:, p0, p1]) elif ndim == 4: for p0 in range(100): for p1 in range(100): for p2 in range(100): do_something(myarray[:, p0, p1, p2]) elif ndim == 5: for p0 in range(100): for p1 in range(100): for p2 in range(100): for p3 in range(100): do_something(myarray[:, p0, p1, p2, p3]) elif ndim == 6: for p0 in range(100): for p1 in range(100): for p2 in range(100): for p3 in range(100): for p4 in range(100): do_something(myarray[:, p0, p1, p2, p3, p4]) Of course this works, but I find the code not very elegant and very much cluttered. Is there a better way to do this? I am pretty sure there must be a way to index the array without knowing a priori the number of dimensions, but I cannot find the proper function in the numpy documentation. Continue reading...