def get_nearest_rhythm(interval, rhythmical_durations):
    """
    Given a certain interval in seconds, gets the rhythmical duration
    that has the lower distance with it.

    :param interval: duration in seconds
    :param rhythmical_durations: dictionary returned by the get_durations
    :return:
    """

    # the keys and the values obtained from the methods "values" and "keys"
    # of a dictionary are correctly ordered, so we can calculate
    # the distances from the keys, get the index of the argmin
    # and use it to get the correct key

    # values of the rhythmical_durations dictionary
    rhythmical_durations_values = list(rhythmical_durations.values())

    # keys of the rhythmical_durations dictionary
    rhythmical_durations_keys = list(rhythmical_durations.keys())

    # list comprehension used to map the distance function to the values
    distances = [abs(interval - x) for x in rhythmical_durations_values]

    # argmin of the distance (an index)
    result_index = distances.index(min(distances))

    # using the index to get the correct rhythmical duration key
    return rhythmical_durations_keys[result_index]