---
title: "How to Iterating NumPy Array?"  
description: "How to Iterating NumPy Array?"  
author: "Ravi Vishwakarma"  
published: 2025-11-12  
updated: 2025-11-12  
canonical: https://www.mindstick.com/interview/34411/how-to-iterating-numpy-array  
category: "python"  
tags: ["python-3.4", "numpy"]  
reading_time: 4 minutes  

---

# How to Iterating NumPy Array?

> Iterating over a [**NumPy array**](https://www.mindstick.com/interview/34402/introduction-to-numpy) means accessing its elements (one by one, or by row/column) using loops or built-in functions.

### 1. Basic Iteration (1-D Array)

If you have a [**1-dimensional** NumPy](https://www.mindstick.com/interview/34403/how-to-access-numpy-array-indexing) array, you can simply loop through it like a Python list:

```python
import numpy as np

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

for x in arr:
    print(x)
```

## Output:

```plaintext
1
2
3
4
5
```

### 2. Iterating a 2-D Array (Row by Row)

In a 2D array, iteration gives **each row** as a sub-array:

```python
arr2d = np.array([[1, 2, 3],
                  [4, 5, 6]])

for row in arr2d:
    print(row)
```

## Output:

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

### 3. Iterating Each Element in a Multi-Dimensional Array

If you want to access **every element** (not just rows), use `np.nditer()`:

```python
arr = np.array([[1, 2, 3],
                [4, 5, 6]])

for x in np.nditer(arr):
    print(x)
```

## Output:

```plaintext
1
2
3
4
5
6
```

### 4. Iterating with Data Type Conversion

You can convert elements while iterating, e.g. from `int` to `float`:

```python
for x in np.nditer(arr, flags=['buffered'], op_dtypes=['float']):
    print(x)
```

### 5. Iterating with Index (Using `np.ndenumerate`)

To access both **index** and **value** during iteration:

```python
for idx, x in np.ndenumerate(arr):
    print(idx, x)
```

## Output:

```plaintext
(0, 0) 1
(0, 1) 2
(0, 2) 3
(1, 0) 4
(1, 1) 5
(1, 2) 6
```

### 6. Iterating Along Specific Axis

You can iterate over a **specific axis** (like columns) using `np.apply_along_axis`:

```python
arr = np.array([[1, 2, 3],
                [4, 5, 6]])

# Iterate column-wise (axis=0)
for col in np.apply_along_axis(lambda x: x, axis=0, arr=arr):
    print(col)
```

## Output:

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

### Summary

| Method | Description |
| --- | --- |
| `for x in arr:` | Iterates row-by-row (for 2D) or element-by-element (for 1D). |
| `np.nditer(arr)` | Iterates over each element in any-dimensional array. |
| `np.ndenumerate(arr)` | Iterates with index and value. |
| `np.apply_along_axis()` | Iterates along a specific axis (e.g. column-wise). |

## Answers

### Answer by Ravi Vishwakarma

> Iterating over a [**NumPy array**](https://www.mindstick.com/interview/34402/introduction-to-numpy) means accessing its elements (one by one, or by row/column) using loops or built-in functions.

### 1. Basic Iteration (1-D Array)

If you have a [**1-dimensional** NumPy](https://www.mindstick.com/interview/34403/how-to-access-numpy-array-indexing) array, you can simply loop through it like a Python list:

```python
import numpy as np

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

for x in arr:
    print(x)
```

## Output:

```plaintext
1
2
3
4
5
```

### 2. Iterating a 2-D Array (Row by Row)

In a 2D array, iteration gives **each row** as a sub-array:

```python
arr2d = np.array([[1, 2, 3],
                  [4, 5, 6]])

for row in arr2d:
    print(row)
```

## Output:

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

### 3. Iterating Each Element in a Multi-Dimensional Array

If you want to access **every element** (not just rows), use `np.nditer()`:

```python
arr = np.array([[1, 2, 3],
                [4, 5, 6]])

for x in np.nditer(arr):
    print(x)
```

## Output:

```plaintext
1
2
3
4
5
6
```

### 4. Iterating with Data Type Conversion

You can convert elements while iterating, e.g. from `int` to `float`:

```python
for x in np.nditer(arr, flags=['buffered'], op_dtypes=['float']):
    print(x)
```

### 5. Iterating with Index (Using `np.ndenumerate`)

To access both **index** and **value** during iteration:

```python
for idx, x in np.ndenumerate(arr):
    print(idx, x)
```

## Output:

```plaintext
(0, 0) 1
(0, 1) 2
(0, 2) 3
(1, 0) 4
(1, 1) 5
(1, 2) 6
```

### 6. Iterating Along Specific Axis

You can iterate over a **specific axis** (like columns) using `np.apply_along_axis`:

```python
arr = np.array([[1, 2, 3],
                [4, 5, 6]])

# Iterate column-wise (axis=0)
for col in np.apply_along_axis(lambda x: x, axis=0, arr=arr):
    print(col)
```

## Output:

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

### Summary

| Method | Description |
| --- | --- |
| `for x in arr:` | Iterates row-by-row (for 2D) or element-by-element (for 1D). |
| `np.nditer(arr)` | Iterates over each element in any-dimensional array. |
| `np.ndenumerate(arr)` | Iterates with index and value. |
| `np.apply_along_axis()` | Iterates along a specific axis (e.g. column-wise). |


---

Original Source: https://www.mindstick.com/interview/34411/how-to-iterating-numpy-array

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
