---
title: "How to access NumPy Array Indexing"  
description: "How to access NumPy Array Indexing"  
author: "ICSM Computer"  
published: 2025-11-05  
updated: 2025-11-05  
canonical: https://www.mindstick.com/interview/34403/how-to-access-numpy-array-indexing  
category: "python"  
tags: ["python-3.4", "numpy", "Python 3"]  
reading_time: 5 minutes  

---

# How to access NumPy Array Indexing

> [**NumPy Array**](https://www.mindstick.com/forum/161982/what-is-numpy) **Indexing** refers to the way you access or modify elements in a NumPy array. It’s similar to Python list indexing but much more powerful — supporting slicing, multi-dimensional access, boolean indexing, and fancy indexing.

### 1. Basic Indexing

Just like Python lists:

```python
import numpy as np

arr = np.array([10, 20, 30, 40, 50])
print(arr[0])   # 10
print(arr[-1])  # 50
```

### 2. 2D Array Indexing

Use `[row, column]`:

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

print(arr2d[0, 0])  # 1  (first row, first column)
print(arr2d[1, 2])  # 6  (second row, third column)
```

You can also access entire rows or columns:

```python
print(arr2d[1])      # [4 5 6]  → 2nd row
print(arr2d[:, 2])   # [3 6 9]  → 3rd column
```

### 3. Slicing

You can extract a range of elements:

```python
arr = np.array([10, 20, 30, 40, 50])
print(arr[1:4])   # [20 30 40]
print(arr[:3])    # [10 20 30]
print(arr[::2])   # [10 30 50] (step = 2)
```

For 2D arrays:

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

print(arr2d[0:2, 1:3])
# [[2 3]
#  [5 6]]
```

### 4. Integer (Fancy) Indexing

Use lists or arrays of indices:

```python
arr = np.array([10, 20, 30, 40, 50])
indices = [0, 2, 4]
print(arr[indices])   # [10 30 50]
```

For 2D arrays:

```python
arr2d = np.array([[10, 20], [30, 40], [50, 60]])
print(arr2d[[0, 2], [1, 0]])  # [20 50]
```

Explanation:

`(0,1)` and `(2,0)` are the chosen coordinates.

### 5. Boolean Indexing

Select elements that satisfy a condition:

```python
arr = np.array([10, 20, 30, 40, 50])
print(arr[arr > 25])  # [30 40 50]
```

You can also combine conditions:

```python
print(arr[(arr > 15) & (arr < 45)])  # [20 30 40]
```

For 2D arrays:

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

### 6. Assigning Values

You can modify elements directly:

```python
arr = np.array([10, 20, 30, 40, 50])
arr[1:4] = 0
print(arr)  # [10  0  0  0 50]
```

Boolean assignment:

```python
arr[arr > 25] = 99
print(arr)  # [10 20 99 99 99]
```

### 7. Using `np.ix_()` for Multi-dimensional Selection

If you want to select specific rows and columns:

```python
arr = np.arange(1, 13).reshape(3, 4)
# arr =
# [[ 1  2  3  4]
#  [ 5  6  7  8]
#  [ 9 10 11 12]]

rows = [0, 2]
cols = [1, 3]
print(arr[np.ix_(rows, cols)])
# [[ 2  4]
#  [10 12]]
```

### Summary Table

| Type | Example | Description |
| --- | --- | --- |
| Basic | `arr[2]` | Access single element |
| 2D | `arr[1, 2]` | Row 1, Column 2 |
| Slice | `arr[1:4]` | Range of elements |
| Fancy | `arr[[0,2,4]]` | Use list of indices |
| Boolean | `arr[arr > 5]` | Filter by condition |

## Answers

### Answer by ICSM Computer

> [**NumPy Array**](https://www.mindstick.com/forum/161982/what-is-numpy) **Indexing** refers to the way you access or modify elements in a NumPy array. It’s similar to Python list indexing but much more powerful — supporting slicing, multi-dimensional access, boolean indexing, and fancy indexing.

### 1. Basic Indexing

Just like Python lists:

```python
import numpy as np

arr = np.array([10, 20, 30, 40, 50])
print(arr[0])   # 10
print(arr[-1])  # 50
```

### 2. 2D Array Indexing

Use `[row, column]`:

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

print(arr2d[0, 0])  # 1  (first row, first column)
print(arr2d[1, 2])  # 6  (second row, third column)
```

You can also access entire rows or columns:

```python
print(arr2d[1])      # [4 5 6]  → 2nd row
print(arr2d[:, 2])   # [3 6 9]  → 3rd column
```

### 3. Slicing

You can extract a range of elements:

```python
arr = np.array([10, 20, 30, 40, 50])
print(arr[1:4])   # [20 30 40]
print(arr[:3])    # [10 20 30]
print(arr[::2])   # [10 30 50] (step = 2)
```

For 2D arrays:

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

print(arr2d[0:2, 1:3])
# [[2 3]
#  [5 6]]
```

### 4. Integer (Fancy) Indexing

Use lists or arrays of indices:

```python
arr = np.array([10, 20, 30, 40, 50])
indices = [0, 2, 4]
print(arr[indices])   # [10 30 50]
```

For 2D arrays:

```python
arr2d = np.array([[10, 20], [30, 40], [50, 60]])
print(arr2d[[0, 2], [1, 0]])  # [20 50]
```

Explanation:

`(0,1)` and `(2,0)` are the chosen coordinates.

### 5. Boolean Indexing

Select elements that satisfy a condition:

```python
arr = np.array([10, 20, 30, 40, 50])
print(arr[arr > 25])  # [30 40 50]
```

You can also combine conditions:

```python
print(arr[(arr > 15) & (arr < 45)])  # [20 30 40]
```

For 2D arrays:

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

### 6. Assigning Values

You can modify elements directly:

```python
arr = np.array([10, 20, 30, 40, 50])
arr[1:4] = 0
print(arr)  # [10  0  0  0 50]
```

Boolean assignment:

```python
arr[arr > 25] = 99
print(arr)  # [10 20 99 99 99]
```

### 7. Using `np.ix_()` for Multi-dimensional Selection

If you want to select specific rows and columns:

```python
arr = np.arange(1, 13).reshape(3, 4)
# arr =
# [[ 1  2  3  4]
#  [ 5  6  7  8]
#  [ 9 10 11 12]]

rows = [0, 2]
cols = [1, 3]
print(arr[np.ix_(rows, cols)])
# [[ 2  4]
#  [10 12]]
```

### Summary Table

| Type | Example | Description |
| --- | --- | --- |
| Basic | `arr[2]` | Access single element |
| 2D | `arr[1, 2]` | Row 1, Column 2 |
| Slice | `arr[1:4]` | Range of elements |
| Fancy | `arr[[0,2,4]]` | Use list of indices |
| Boolean | `arr[arr > 5]` | Filter by condition |


---

Original Source: https://www.mindstick.com/interview/34403/how-to-access-numpy-array-indexing

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
