Numpy | Iterating Over Array - GeeksforGeeks

15 Nov, 2018
NumPy package contains an iterator object numpy.nditer. It is an efficient multidimensional iterator object using which it is possible to iterate over an array. Each element of an array is visited using Python’s standard Iterator interface.
Output:
Original array is:
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]

Modified array is:
0 1 2 3 4 5 6 7 8 9 10 11

The order of iteration is chosen to match the memory layout of an array, without considering a particular ordering. This can be seen by iterating over the transpose of the above array.
Output:
Original array is:
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]

Modified array is:
0 1 2 3 4 5 6 7 8 9 10 11

 
Controlling Iteration Order:There are times when it is important to visit the elements of an array in a specific order, irrespective of the layout of the elements in memory. The nditer object provides an order parameter to control this aspect of iteration. The default, having the behavior described above, is order=’K’ to keep the existing order. This can be overridden with order=’C’ for C order and order=’F’ for Fortran order.
Code #1:
Output:
Original array is:
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]

Modified array in C-style order:
0 1 2 3 4 5 6 7 8 9 10 11

 Code #2:
Output:
Original array is:
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]

Modified array in F-style order:
0 4 8 1 5 9 2 6 10 3 7 11

 
Modifying Array Values:The nditer object has another optional parameter called op_flags. Its default value is read-only, but can be set to read-write or write-only mode. This will enable modifying array elements using this iterator.
Output:
Original array is:
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]

Modified array is:
[[ 0 5 10 15]
 [20 25 30 35]
 [40 45 50 55]]

 
External Loop:
The nditer class constructor has a flags parameter, which can take the following values
Code #1:
Output:
Original array is:
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]

Modified array is:
[ 0 1 2 3] [ 4 5 6 7] [8 9 10 11] 

 
Code #2:
Output:
Original array is:
[[ 0 1 2]
 [ 3 4 5]]

0 <0> 1 <2> 2 <4> 3 <1> 4 <3> 5 <5>

 
Broadcasting Iteration:If two arrays are broadcastable, a combined nditer object is able to iterate upon them concurrently. Assuming that an array a has dimension 3X4, and there is another array b of dimension 1X4, the iterator of following type is used (array b is broadcast to size of a).
Output:
First array is:
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]

Second array is:
[5 6 7 8]

Modified array is:
0:5 1:6 2:7 3:8 4:5 5:6 6:7 7:8 8:5 9:6 10:7 11:8

 
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Java Program for Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
Python Program for Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
Java Program for Depth First Search or DFS for a Graph
Python Program for Depth First Search or DFS for a Graph
Java Program for Breadth First Search or BFS for a Graph
Python Program for Breadth First Search or BFS for a Graph
C Program for Coin Change | DP-7
Java Program for Coin Change | DP-7
PHP Program for Coin Change | DP-7
C Program for Find the number of islands | Set 1 (Using DFS)