Pythonic way to iterate through multidimensional space?

Peter Otten __peter__ at web.de
Wed Aug 6 03:39:42 EDT 2014


Gayathri J wrote:

> Dear Peter
> 
> Below is the code I tried to check if itertools.product() was faster than
> normal nested loops...
> 
> they arent! arent they supposed to be...

I wouldn't have expected product() to be significantly faster, but neither 
did I expect it to be slower.

> or am i making a mistake? any
> idea?

For your testcase you can shave off a small amount by avoiding the tuple-
unpacking

for t in product(...):
    f[t] = 0.0

but that may be cheating, and the effect isn't big.

When you are working with numpy there may be specialised approaches, but

    f[:,:,:] = 0.0

is definitely cheating I suppose...




More information about the Python-list mailing list