Here are some answers that work without knowing the dimension in advance.
A recursive version:
def multiget_rec(arr, *indices):
if len(indices)==0:
return arr
return multiget_rec(arr[indices[0]], *indices[1:])
A procedural version:
def multiget_proc(arr, *indices):
while len(indices)>0:
i, *indices = indices
arr = arr[i]
return arr
And a version based on reduce which could be used as a 1-liner:
from functools import reduce
def multiget_reduce(arr, *indices):
return reduce(lambda a,i:a[i], indices, arr)
All can be called like
for i in index:
print (multiget(elements, *i))
Edit:
Using reduce seems to be the fastest of the three methods, at least for small arrays. I also think it looks the cleanest.
$ python -m timeit -s "def multiget_rec(arr, *indices):" \
-s " if len(indices)==0: return arr" \
-s " return multiget_rec(arr[indices[0]], *indices[1:])" \
-- "d = [[[3]]]" \
"multiget_rec(d, 0,0,0)"
500000 loops, best of 5: 941 nsec per loop
$ python -m timeit -s "def multiget_proc(arr, *indices):" \
-s " while len(indices)>0:" \
-s " i, *indices = indices" \
-s " return arr" \
-s "d = [[[3]]]" \
-- "multiget_proc(d, 0,0,0)"
500000 loops, best of 5: 529 nsec per loop
$ python -m timeit -s "from functools import reduce" \
-s "def multiget_reduce(arr, *indices):" \
-s " return reduce(lambda a,i:a[i], indices, arr)" \
-s "d = [[[3]]]" \
-- "multiget_reduce(d, 0,0,0)"
1000000 loops, best of 5: 384 nsec per loop