Using random in the lambda or list comprehension means a new random number is generated for each key of the object as we iterate over them. This is not what we want if we want to find the closest key to the random number
It is also very slow. So this 2 changes have the following effect:
city is looked up from a single random number, and much much faster.
age look up is simplified by using the same type of pattern.
Note 1 : the commented code was close, but wrong. My guess is the more complex code was written as a workaround, so this is the fix for it.
Using random in the lambda or list comprehension means a new random number is generated for each key of the object as we iterate over them. This is not what we want if we want to find the closest key to the random number
It is also very slow. So this 2 changes have the following effect:
Note 1 : the commented code was close, but wrong. My guess is the more complex code was written as a workaround, so this is the fix for it.