Collections Counter module issue with most_common class
<p>I have a dictionary made from a List called ltst3_upper, and now I am trying to use the class most_common to get only the top 10 key:values with the below code:</p>

<pre><code>result3  = Counter(list3_upper).most_common(10)
sort_result3 = OrderedDict(sorted(result3.items(), key=operator.itemgetter(1), reverse=True))
</code></pre>

<p>I am lopping this code in several Lists which are 'organized' with Counter, and some of them have more than 10 keys:values, so I want to trim the Dict.</p>

<p>Ps. as you can see besides the top 10 values I am also ordering the from highest to smallest, but i dont think the problem is there.</p>

<p>The error is:</p>

<pre><code>AttributeError: 'list' object has no attribute 'items'
</code></pre>

<p>Is this happening because some Lists do not have 10 keys:values?</p>

<p>Thanks a lot for any input you might have.</p>

<p><a href="https://docs.python.org/2/library/collections.html#collections.Counter.most_common" rel="nofollow">As the documentation states,</a><code>most_common</code> returns a list of the most common elements.  <code>.items</code> is a <code>dict</code> method - lists don't have items.  If you want to do something to all members in the list, you'd iterate over them..:</p>

<pre><code>for result in result3:
    o = OrderedDict(sorted(result.items(), key=operator.itemgetter(1), reverse=True))
</code></pre>

<p><strong>but</strong> this won't work either -  the individual members of the list are <code>tuple</code>, not <code>dict</code> - and <code>tuple</code> objects don't have an <code>items</code> method. Instead, just create the <code>OrderedDict</code> with <code>result</code>:</p>

<pre><code>from collections import Counter, OrderedDict
import operator

list3_upper = ['a', 'e', 'a']

result3  = Counter(list3_upper).most_common(10)

result_dict = OrderedDict(result3)
print(result_dict)
&gt;&gt;&gt; OrderedDict([('a', 2), ('e', 1)])
</code></pre>

python