I found an interesting way iterate through Numpy arrays. Code below:

```
import numpy as np
arr = np.random.rand(1000)
mean = np.mean(arr)
for x in np.nditer(arr, op_flags = ['readwrite']):
x[...] = x - mean
np.mean(arr)
```

comments?

I found an interesting way iterate through Numpy arrays. Code below:

```
import numpy as np
arr = np.random.rand(1000)
mean = np.mean(arr)
for x in np.nditer(arr, op_flags = ['readwrite']):
x[...] = x - mean
np.mean(arr)
```

comments?

2 Likes

Yes, `nditer`

gives sophisticated options for iterations.

But for simple cases for the one you have shown in the example, it would be enough to iterate it naively using `for`

.

Iām not sure if using `nditer`

would be advantageous than without it. Indeed both variants would be considered as a type of `Iterator`

in python which would have been optimized the same way under the hood (in numpy C core)

Thank you very much for your remarks. I timed nditer and it certainly was much slower for this piece of code.

I merely tried nditer as another variance merely from a learning perspective.