Finding Set of strings from list of pairs (string, int) with maximum int value
<p>I have a list of <code>(str,int)</code> pairs </p>

<p><code>list_word = [('AND', 1), ('BECAUSE', 1), ('OF', 1), ('AFRIAD', 1), ('NEVER', 1), ('CATS', 2), ('ARE', 2), ('FRIENDS', 1), ('DOGS', 2)]</code></p>

<p>This basically says how many times each word showed up in a text. </p>

<p>What I want to get is the set of words with maximum occurrence along with maximum occurrence number. So, in the above example, I want to get</p>

<p><code>(set(['CATS', 'DOGS','ARE']), 2)</code></p>

<p>The solution I can think of is looping through the list. But is there any elegant way of doing this?</p>

<p>Through the use of <a href="https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions" rel="nofollow" title="list comprehensions">list comprehensions</a> you can first select the maximum occurrence and then filter the strings accordingly</p>

<pre><code>list = [('AND', 1), ('BECAUSE', 1), ('OF', 1), ('AFRIAD', 1), ('NEVER', 1), ('CATS', 2), ('ARE', 2), ('FRIENDS', 1), ('DOGS', 2)]

mx = max([x[1] for x in list])
res = (set([x[0] for x in list if x[1]==mx]), mx)
</code></pre>

python string set max pair