Python find float nearest value in list of tuple

I have a value total_weight = 212.0 and I have a section of my program that tries to find the exact value but I don’t have the exact weight in my list.

So I want to get the nearest value. In this example it would be [(200.0, (2.5, 5.0, 10.0, 15.0, 45.0)), (200.0, (2.5, 5.0, 10.0, 25.0, 35.0)), (220.0, (2.5, 5.0, 10.0, 25.0, 45.0))]

List = [ (180.0, (2.5, 5.0, 10.0, 15.0, 35.0)), (200.0, (2.5, 5.0, 10.0, 15.0, 45.0)), (200.0, (2.5, 5.0, 10.0, 25.0, 35.0)), (220.0, (2.5, 5.0, 10.0, 25.0, 45.0)), (240.0, (2.5, 5.0, 10.0, 35.0, 45.0)), (210.0, (2.5, 5.0, 15.0, 25.0, 35.0)), (230.0, (2.5, 5.0, 15.0, 25.0, 45.0)), (250.0, (2.5, 5.0, 15.0, 35.0, 45.0)), (270.0, (2.5, 5.0, 25.0, 35.0, 45.0)), (220.0, (2.5, 10.0, 15.0, 25.0, 35.0)), (240.0, (2.5, 10.0, 15.0, 25.0, 45.0)), (260.0, (2.5, 10.0, 15.0, 35.0, 45.0)), (280.0, (2.5, 10.0, 25.0, 35.0, 45.0)), (290.0, (2.5, 15.0, 25.0, 35.0, 45.0)), (225.0, (5.0, 10.0, 15.0, 25.0, 35.0)), (245.0, (5.0, 10.0, 15.0, 25.0, 45.0)), (265.0, (5.0, 10.0, 15.0, 35.0, 45.0)))]

I have tried with min() but I haven’t been able to make it work.

Answer

To find the nearest value, use min with a key function that gives the distance from the target:

>>> weights = [
    (180.0, (2.5, 5.0, 10.0, 15.0, 35.0)), 
    (200.0, (2.5, 5.0, 10.0, 15.0, 45.0)), 
    (200.0, (2.5, 5.0, 10.0, 25.0, 35.0)), 
    (220.0, (2.5, 5.0, 10.0, 25.0, 45.0)), 
    (240.0, (2.5, 5.0, 10.0, 35.0, 45.0)), 
    (210.0, (2.5, 5.0, 15.0, 25.0, 35.0)), 
    (230.0, (2.5, 5.0, 15.0, 25.0, 45.0)), 
    (250.0, (2.5, 5.0, 15.0, 35.0, 45.0)), 
    (270.0, (2.5, 5.0, 25.0, 35.0, 45.0)), 
    (220.0, (2.5, 10.0, 15.0, 25.0, 35.0)), 
    (240.0, (2.5, 10.0, 15.0, 25.0, 45.0)), 
    (260.0, (2.5, 10.0, 15.0, 35.0, 45.0)), 
    (280.0, (2.5, 10.0, 25.0, 35.0, 45.0)), 
    (290.0, (2.5, 15.0, 25.0, 35.0, 45.0)), 
    (225.0, (5.0, 10.0, 15.0, 25.0, 35.0)), 
    (245.0, (5.0, 10.0, 15.0, 25.0, 45.0)), 
    (265.0, (5.0, 10.0, 15.0, 35.0, 45.0)),
]
>>> total_weight = 212.0
>>> min(weights, key=lambda w: abs(w[0] - total_weight))
(210.0, (2.5, 5.0, 15.0, 25.0, 35.0))

To find the higher and lower value, sort and find the index of the first higher value, then take that and the previous:

>>> weights.sort()
>>> higher = next(i for i in range(len(weights)) if weights[i][0] > total_weight)
>>> weights[higher-1:higher+1]
[(210.0, (2.5, 5.0, 15.0, 25.0, 35.0)), (220.0, (2.5, 5.0, 10.0, 25.0, 45.0))]

(Handling edge cases adds more complexity — what if there is an exact match? What if the target either higher or lower than all the values? Etc.)