numpy
value comparison
array operations
python programming
data analysis

NumPy: find the first index where a value is greater than a threshold

Codemia Knowledge Hub ·

For an unsorted array, build a boolean mask (arr > threshold) and take the first True index with np.flatnonzero or a guarded np.argmax. For a sorted array, use np.searchsorted(arr, threshold, side="right"), which does a binary search.

Unsorted arrays: use a boolean mask

python
1import numpy as np
2
3arr = np.array([3, 1, 4, 2, 9, 5])
4threshold = 4
5
6mask = arr > threshold
7indices = np.flatnonzero(mask)
8
9if indices.size > 0:
10    first_index = indices[0]
11    first_value = arr[first_index]
12    print(first_index, first_value)
13else:
14    print("No value greater than threshold")

This is explicit and handles the "not found" case cleanly.

Why np.argmax needs a guard

You will often see this pattern:

python
first_index = np.argmax(arr > threshold)

It works only if at least one element satisfies the condition. If none does, np.argmax returns 0, the first position of an all-false mask, which looks like a valid match. Check any() first:

python
1mask = arr > threshold
2if mask.any():
3    first_index = np.argmax(mask)
4    print(first_index, arr[first_index])
5else:
6    print("No match")

This is concise and often faster than extracting all indices when you only need the first one.

Sorted arrays: use searchsorted

If the array is sorted in ascending order, np.searchsorted performs a binary search and is the idiomatic solution.

python
1arr = np.array([1, 2, 4, 4, 7, 9])
2threshold = 4
3
4idx = np.searchsorted(arr, threshold, side="right")
5
6if idx < arr.size:
7    print(idx, arr[idx])
8else:
9    print("No value greater than threshold")

side="right" returns the insertion point after any values equal to the threshold, so the element at that index is the first one strictly greater. Use it to get > rather than >= behavior when there are duplicates.

Using another element as the threshold

Sometimes the threshold comes from an array element or a paired dataset:

python
1arr = np.array([10, 12, 15, 18, 30])
2reference_index = 2
3reference_value = arr[reference_index]
4
5idx = np.searchsorted(arr, reference_value, side="right")
6print(idx, arr[idx])

This finds the first value greater than arr[2], assuming the array is sorted. searchsorted assumes sorted order, so on an unsorted array its result is meaningless.

Choosing between the approaches

Use flatnonzero or a checked argmax when:

  • The array is unsorted
  • You care about the first match in original order
  • Simplicity matters more than asymptotic speed

Use searchsorted when:

  • The array is sorted
  • You want the first value greater than a threshold efficiently
  • You may repeat the query many times

Whether the array is sorted is the key decision. For one-off scans on modest arrays, readability matters more than micro-optimizing the search primitive. Recognizing sorted data is what enables searchsorted and changes the algorithmic cost.


Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design