I often find myself doing this:
for x in range(x_size):
for y in range(y_size):
for z in range(z_size):
pass # do something here
Is there a more concise way to do this in Python? I am thinking of something along the lines of
for x, z, y in ... ? :
You can use itertools.product:
Use
itertools.product()
:From the docs:
It depends on what is inside the loop. If dealing with lists, you may be able to use a list comprehension
For the more general case, see this post on itertools.
If you've got
numpy
as a dependency already,numpy.ndindex
will do the trick ...