def closest_length(refs, pred):
    '''
    >>> closest_length(['1234', '12345', '1'], '123')
    4
    >>> closest_length(['123', '12345', '1'], '12')
    1
    '''
    smallest_diff = float('inf')
    closest_length = float('inf')
    for ref in refs:
        diff = abs(len(ref) - len(pred))
        if diff < smallest_diff or (diff == smallest_diff and len(ref) < closest_length):
            smallest_diff = diff
            closest_length = len(ref)
    return closest_length
def closest_length ( refs , pred ) : smallest_diff = float ( 'inf' ) closest_length = float ( 'inf' ) for ref in refs : diff = abs ( len ( ref ) - len ( pred ) ) if diff < smallest_diff or ( diff == smallest_diff and len ( ref ) < closest_length ) : smallest_diff = diff closest_length = len ( ref ) return closest_length
>>> closest_length(['1234', '12345', '1'], '123')
    4
    >>> closest_length(['123', '12345', '1'], '12')
    1