Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: get the corresponding key from value

def get_nearest_less_element(d, k):
    return (min(value for value in map(float, d.values()) if value >= k))

So, d is a dict() and k is float, this function returns me the correct value, but can I return the corresponding key of that value from this function?

like image 605
MrRobot9 Avatar asked Apr 09 '26 20:04

MrRobot9


1 Answers

def get_nearest_less_element(d, k):
    return min(key for key, value in d.items() if float(value) >= k)
like image 87
Deepstop Avatar answered Apr 11 '26 10:04

Deepstop