Python: "if closest to 1"
Question: I am doing some calculations on a dictionary. But the important thing is I
want to make a if-condition that kind of says

"if **x** has a value that is closer to 1 (or equal to 1) than **variable** "

kind of hard to explain, but hope you understand.


Answer: You can use [absolute
value](http://docs.python.org/2/library/functions.html#abs):

    
    
    if abs(x-1) < abs(variable-1):
        ...
    

since absolute value of `x-1` is the distance between `x` and `1`, and
similarly absolute value of `variable-1` is the distance between `variable`
and `1`.

