I have a data in form a dictionary.. NOw I take the input from the user and it can be anything.. And I am trying to do the following. If the key exists then cool.. fetch the
This should do what you want (minus getting it from a key, but you can figure that out :).
f = lambda a,l:min(l,key=lambda x:abs(x-a)) numbers = (100, 200, 300, 400) num = int(raw_input()) print 'closest match:', f(num, numbers)
Note: f is from this question.
f