 Introduction to Python NumPy Iterating

Python Numpy Iterating – Before moving ahead, let’s know a little bit about Python NumPy Shape

Iterating is the act of going through each element one-by-one.

We can use the basic for loop of Python to deal with multi-dimensional arrays within numpy.

Example – Iterating on a 1-D array will pass through each element one-by-one.

```import numpy as np

Array = np.array([1, 2, 3])
for x in Array:
print(x)```
```Output -

1
2
3```

As a result, it returned every element of an array.

Iterating 2-D Arrays

In a 2-D array, it will go through all the rows.

```import numpy as np

Array = np.array([[1, 2, 3], [4, 5, 6]])
for x in Array:
print(x)```
```Output -

[1 2 3]
[4 5 6]```

As a result, it iterated every element of the 2-D array, therefore, returned every single element of the 2-D array.

Note: Iterating on an nth-D array will result in iterating through each dimension.

We must iterate each dimension to return the actual values.

Example – Iterate throughout each element of the 2-D array.

```import numpy as np

Array = np.array([[1, 2, 3], [4, 5, 6]])
for x in Array:
for y in x:
print(y)```
```Output -

1
2
3
4
5
6```

As can be seen, it returned every single element of the array by iterating through the ‘for’ loop.

Iterating 3-D Arrays

In a 3-D array, it will go through all the 2-D arrays.

Example – Iterate on the elements of the 3-D array.

```import numpy as np

Array = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
for x in Array:
print(x)```
```Output -

[[1 2 3]
[4 5 6]]
[[ 7 8 9]
[10 11 12]]```

As shown above, it iterated throughout every single element of the 3-D array.

Note: We have to iterate the arrays in each dimension, to return the actual values.

Example – Iterating down to each element.

```import numpy as np

Array = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

for x in Array:
for y in x:
for z in y:
print(z)```
```Output -

1
2
3
4
5
6
7
8
9
10
11
12```

As has been noted, it returned every single element from an array after iterating through each element.

Arrays Iterating Using nditer()

Function nditer() can be used for very simple to very complex iterations. Let’s look at some examples to illustrate how it solves basic problems in iteration.

Iterating on Each Array Element

Basic for loops require us to iterate through each element in an array. This can make it difficult to write code for large arrays.

Example – Iterate through each element of the 3-D array

```import numpy as np

Array = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
for x in np.nditer(Array):
print(x)```
```Output -

1
2
3
4
5
6
7
8```

The final analysis, iterated through each element of the 3-D array.

Iterating an array with different data types

You can use the op_dtypes argument to pass the expected data type while iterating.

NumPy doesn’t change the data type of an element in place (elements are in array). This is why it requires buffer space. To enable buffer in nditer(), we pass flags=[“buffered]].

Example – Iterate through each element of the array as a string data type.

```import numpy as np

Array = np.array([1, 2, 3])
for x in np.nditer(Array, flags=['buffered'], op_dtypes=['S']):
print(x)```
```Output -

b'1'
b'2'
b'3'```

In short, it iterated through each element of the array as a string data type.

Iterating with skipping element

Example – Skipping 1 element while iterating through the array.

```import numpy as np

Array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
for x in np.nditer(Array[:, ::2]):
print(x)```
```Output -

1
3
5
7```

As shown above, it returned every element of an array an interval of one element.

Enumerated Iteration using ndenumerate()

Enumeration is the act of referring to a sequence number of things one by one.

Sometimes, we need the element’s index while iterating.

Example – Iterating through elements of 1D arrays while taking out its index number.

```import numpy as np

Array = np.array([1, 2, 3])
for z, x in np.ndenumerate(Array):
print(z, x)```

As has been noted, it iterated elements of 1D arrays while taking out its index number.

Example – Iterating through elements of 2D arrays while taking out its index number.

```import numpy as np

Array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
for z, x in np.ndenumerate(Array):
print(z, x)```
```Output -

(0, 0) 1
(0, 1) 2
(0, 2) 3
(0, 3) 4
(1, 0) 5
(1, 1) 6
(1, 2) 7
(1, 3) 8```

As can be seen, it iterated elements of 2D arrays while taking out its index number.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on