given a set of integers and a value for which you need to find the nearest one.
def find_nearest(s, target): if target in s: return target diff = [] for val in s: diff.append(abs(target - val)) nearest = diff.index(min(diff)) return s[nearest] find_nearest([1, 2, 3, 4, 5], 6)