Python Get top values in dictionary
<p>I have a dictionary called <code>wordCounts</code> which maps a word to how many times it occurred, how can I get the top <code>n</code> words in the dict while allowing more than <code>n</code> if there is a tie?</p>

<p>MooingRawr is on the right track, but now we need to just get the top <code>n</code> results</p>

<pre><code>l = []
for i, (word, count) in enumerate(sorted(d.items(), reverse=True, key=lambda x: x[1])):  
    if i &gt;= n and count&lt;l[-1][1]:
        break
    l.append((word, count))
</code></pre>

python dictionary