Get greatest key in dictionary but lower than a variable
<p>Given a dictionary and a variable like this:</p>

<pre><code>dic = {0 : 'some', 10 : 'values', 20 : 'whatever'}
var = 14
</code></pre>

<p>I want to get the value from the dictionary where the key is the greatest but lower or equal to the variable. I don't know if that's clear but in this case I am looking for <code>10</code>.</p>

<p>I've come up with this function but I wanted to know if there is an easier way to do it as I am pretty new to python.</p>

<pre><code>def get_greatest_key(dic, var):
    # if the key exists no need to search
    if var in dic:
        return dic[var]
    else:
        # create a list with all the keys sorted in reverse 
        l = sorted(dic, key=dic.get)
        i = 0
        while i &lt; len(l):
            # parse the list
            if l[i] &lt;= var:
                return dic[l[i]]
            i += 1
        # by default we return the last element in the dictionary
        return dic[l[len(l) - 1]]
</code></pre>
