PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteUse min() with an absolute-distance key to get the closest value from a Python iterable. If you also need the position in a NumPy array, subtract the target, take absolute values, and use argmin() to find the index.
Find the closest value in a Python list
For a list or other iterable of numeric values, pass min() a key function that measures each value’s distance from the target:
values = [1, 5, 9, 14]
target = 8
closest = min(values, key=lambda x: abs(x - target))
print(closest) # 9
The key function computes abs(x - target) for each item, so min() returns the original item with the smallest distance—not the distance itself. The method works with comparable numeric values and scans the iterable once. Python’s built-in functions documentation specifies that when multiple items are minimal, min() returns the first one encountered.
Handle an empty iterable
If the iterable might be empty, min() raises ValueError unless you provide default. Choose a default that makes sense for the rest of your program:
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →#1 Best Overall
closest = min(values, key=lambda x: abs(x - target), default=None)
Alternatively, check that the collection contains values before calling min().
Get the closest value and index in a NumPy array
With NumPy, use argmin() on the absolute differences to get the position, then index the original array to retrieve the value:
Rank #2
import numpy as np
arr = np.array([1, 5, 9, 14])
target = 8
idx = np.abs(arr - target).argmin()
closest = arr[idx]
print(idx) # 2
print(closest) # 9
idx is the index of the closest element, while closest is the element stored there. The NumPy 2.2 argmin reference documents that ties return the index of the first occurrence.
Multidimensional arrays
Without an axis argument, argmin() returns an index into the flattened array. To find a closest value separately along rows or columns, specify the relevant axis. If you use the default flattened result but need coordinates in the original dimensions, convert the index with numpy.unravel_index().
Check for an empty array
Check that a NumPy array has elements before calling argmin(). An empty array has no minimum, so decide whether your application should return a fallback value or raise an error before performing the reduction.
Choose the approach that matches your data
- Python list or general iterable; value only: use
min(values, key=lambda x: abs(x - target)). It needs no NumPy dependency. - NumPy array; index and value: use
np.abs(arr - target).argmin()for the index, then accessarr[idx]. - Sorted sequence with repeated queries: use
bisect_left()to find where the target would be inserted, then compare the neighboring values. This approach relies on the sequence already being sorted.
Python’s bisect documentation describes bisect_left() as locating an insertion point that separates values less than the target from values greater than or equal to it. When using it, account for insertion positions at both ends of the sequence, where only one neighbor exists.
Decide how ties and special values should work
Equal distances
Both min() and NumPy’s argmin() choose the first encountered minimum. For example, with values [5, 9] and target 7, both are equally close, so the first item wins. If your application should instead prefer the smaller value or apply another rule, encode that rule explicitly in your selection logic.
NaN values
If the data may contain NaNs, do not assume ordinary argmin() will ignore them. Decide whether NaNs should be excluded or treated as a special case, and use an appropriate NaN-aware NumPy operation if that matches your intended behavior.
Free tools Windows power users keep installed
One-click scans. No signup required.
Best Value
Define distance for non-scalar data
These examples use one-dimensional numeric distance: abs(value - target). For points, vectors, or domain-specific values, choose the distance metric that represents closeness in your application before selecting a minimum.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




