A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/numpy-iterating-over-array/ below:

Numpy - Iterating Over Arrays

Numpy - Iterating Over Arrays

Last Updated : 23 Jan, 2025

NumPy provides flexible and efficient ways to iterate over arrays of any dimensionality. For a one-dimensional array, iterating is straightforward and similar to iterating over a Python list.

Let's understand with the help of an example:

Python
import numpy as np

# Create a 1D array
arr = np.array([1, 2, 3, 4, 5])

# Iterate over the array
for ele in arr:
    print(ele)

Explanation:

Let's explore various others ways to iterate over Arrays:

Iteration Over a Two-Dimensional Array

For multidimensional arrays, iteration is performed over the first axis by default. Each iteration yields a one-dimensional sub-array.

Python
import numpy as np 

# Create a 2D array
arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Iterate over rows
for row in arr_2d:
    print(row)

Output
[1 2 3]
[4 5 6]
[7 8 9]

Explanation:

For Element-Wise Iteration

To iterate over individual elements of a 2D array, the array can be flattened using the .flat attribute.

Python
import numpy as np

arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

for element in arr_2d.flat:
    print(element)
Iterating Over Higher-Dimensional Arrays

Iteration is similar for arrays with more than two dimensions. In such cases, iteration occurs along the first axis by default.

Python
import numpy as np

# Create a 3D array
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

# Iterate over the 3D array
for sub_array in arr_3d:
    print(sub_array)

Output
[[1 2]
 [3 4]]
[[5 6]
 [7 8]]

Explanation:

Using nditer

The numpy.nditer object offers a various way to iterate over arrays. It allows iteration in different orders and provides better control over the iteration process.

Python
import numpy as np

# Using nditer
arr = np.array([[1, 2], [3, 4]])

for element in np.nditer(arr):
    print(element)

We can also specify the order of iteration (row-major order 'C' or column-major order 'F'):

Python
import numpy as np

arr = np.array([[1, 2], [3, 4]])

for element in np.nditer(arr, order='F'):
    print(element)
Using Enumerate

For multidimensional arrays, we can use enumerate to access both the index and value during iteration.

Python
import numpy as np

# Create a 2D array
arr = np.array([[10, 20], [30, 40]])

# Enumerate through rows
for idx, row in enumerate(arr):
    print(f"Row {idx}: {row}")

Output
Row 0: [10 20]
Row 1: [30 40]
Using Broadcasting

When working with arrays of different shapes, Broadcasting allows iteration across combined shapes seamlessly.

Python
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([[10], [20]])

# Perform a broadcasted addition
for x, y in np.nditer([arr1, arr2]):
    print(f"{x} + {y} = {x + y}")

Output
1 + 10 = 11
2 + 10 = 12
3 + 10 = 13
1 + 20 = 21
2 + 20 = 22
3 + 20 = 23


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4