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
This is explicit and handles the "not found" case cleanly.
Why np.argmax needs a guard
You will often see this pattern:
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:
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.
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:
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.
Related reading
- numpy generate data from linear function
- Numpy Get random set of rows from 2D array
- NumPy grouping using itertools.groupby performance
- numpy How can I select specific indexes in an np array for k-fold cross validation?
- NumPy or Pandas Keeping array type as integer while having a NaN value
- O1 algorithm to determine if node is descendant of another node in a multiway tree?
- Numpy is installed but still getting error
- Numpy linear regression with regularization
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

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 courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.