---
title: "How to search in NumPy Arrays?"  
description: "How to search in NumPy Arrays?"  
author: "Anubhav Sharma"  
published: 2025-11-17  
updated: 2025-11-17  
canonical: https://www.mindstick.com/interview/34414/how-to-search-in-numpy-arrays  
category: "python"  
tags: ["python-3.4", "numpy"]  
reading_time: 4 minutes  

---

# How to search in NumPy Arrays?

> **Searching** [**Arrays in NumPy**](https://www.mindstick.com/interview/34403/how-to-access-numpy-array-indexing) — how to find values, indexes, and conditions inside arrays.

## 1. `np.where()` — Find Indexes of Matching Values

This is the most common function used for searching.

### Example: Find indexes of value 5

```python
import numpy as np

arr = np.array([1, 5, 7, 5, 9])

result = np.where(arr == 5)
print(result)
```

## Output:

```plaintext
(array([1, 3]),)
```

Indexes **1** and **3** contain value **5**.

## 2. Search for Values Based on Condition

### Example: Find all numbers greater than 6

```python
print(np.where(arr > 6))
```

## Output:

```plaintext
(array([2, 4]),)
```

## 3. Extract Elements Matching a Condition

Instead of indexes, get the elements directly:

```python
print(arr[arr > 6])
```

## Output:

```plaintext
[7 9]
```

## 4. Search in a 2D Array

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

# find where value is 5
print(np.where(arr2 == 5))
```

## Output:

```plaintext
(array([1]), array([1]))
```

This means row **1**, column **1**.

## 5. `np.searchsorted()` — Searching Sorted Arrays

Used to find where a value should be inserted to keep an array sorted.

```python
arr = np.array([10, 20, 30, 40])

print(np.searchsorted(arr, 25))
```

## Output:

```plaintext
1
```

25 would go at **index 1** to keep the array sorted.

### Insert left or right position

```python
print(np.searchsorted(arr, 30, side="right"))
```

## Output:

```plaintext
3
```

## 6. Find All Non-zero Elements

```python
arr = np.array([0, 3, 0, 4, 5])

print(np.nonzero(arr))
```

## Output:

```plaintext
(array([1, 3, 4]),)
```

## 7. Check If Any / All Conditions Match

### Check if any element matches condition:

```python
print(np.any(arr > 4))
```

Output:

```plaintext
True
```

### Check if all elements match:

```python
print(np.all(arr > 0))
```

Output:

```plaintext
False
```

## Summary Table

| Function | Usage |
| --- | --- |
| `np.where()` | Find indexes matching a condition |
| `np.nonzero()` | Find all non-zero elements |
| `np.searchsorted()` | Find position in sorted array |
| `arr[condition]` | Get elements matching condition |
| `np.any()` | Checks if any condition is true |
| `np.all()` | Checks if all conditions are true |

## Answers

### Answer by Anubhav Sharma

> **Searching** [**Arrays in NumPy**](https://www.mindstick.com/interview/34403/how-to-access-numpy-array-indexing) — how to find values, indexes, and conditions inside arrays.

## 1. `np.where()` — Find Indexes of Matching Values

This is the most common function used for searching.

### Example: Find indexes of value 5

```python
import numpy as np

arr = np.array([1, 5, 7, 5, 9])

result = np.where(arr == 5)
print(result)
```

## Output:

```plaintext
(array([1, 3]),)
```

Indexes **1** and **3** contain value **5**.

## 2. Search for Values Based on Condition

### Example: Find all numbers greater than 6

```python
print(np.where(arr > 6))
```

## Output:

```plaintext
(array([2, 4]),)
```

## 3. Extract Elements Matching a Condition

Instead of indexes, get the elements directly:

```python
print(arr[arr > 6])
```

## Output:

```plaintext
[7 9]
```

## 4. Search in a 2D Array

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

# find where value is 5
print(np.where(arr2 == 5))
```

## Output:

```plaintext
(array([1]), array([1]))
```

This means row **1**, column **1**.

## 5. `np.searchsorted()` — Searching Sorted Arrays

Used to find where a value should be inserted to keep an array sorted.

```python
arr = np.array([10, 20, 30, 40])

print(np.searchsorted(arr, 25))
```

## Output:

```plaintext
1
```

25 would go at **index 1** to keep the array sorted.

### Insert left or right position

```python
print(np.searchsorted(arr, 30, side="right"))
```

## Output:

```plaintext
3
```

## 6. Find All Non-zero Elements

```python
arr = np.array([0, 3, 0, 4, 5])

print(np.nonzero(arr))
```

## Output:

```plaintext
(array([1, 3, 4]),)
```

## 7. Check If Any / All Conditions Match

### Check if any element matches condition:

```python
print(np.any(arr > 4))
```

Output:

```plaintext
True
```

### Check if all elements match:

```python
print(np.all(arr > 0))
```

Output:

```plaintext
False
```

## Summary Table

| Function | Usage |
| --- | --- |
| `np.where()` | Find indexes matching a condition |
| `np.nonzero()` | Find all non-zero elements |
| `np.searchsorted()` | Find position in sorted array |
| `arr[condition]` | Get elements matching condition |
| `np.any()` | Checks if any condition is true |
| `np.all()` | Checks if all conditions are true |


---

Original Source: https://www.mindstick.com/interview/34414/how-to-search-in-numpy-arrays

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
