I can't use the sorted function to sort a two elements tuple list in descending numerical order then in ascending alphabetical order in case of a tie
<p>I used the sorted() function to sort a list of tuples, as it is shown below. the output is in descending order as wanted for the second elements (numbers). but what I really want is to sort the tuples in ascending alphabetical order in case of a tie.</p>

<p>Help please. </p>

<pre><code>sorted(word_Count, key = itemgetter(1,0), reverse = True)
</code></pre>

<p>Output: </p>

<pre><code>[('butter', 2), ('was', 1), ('the', 1), ('of', 1), ('but', 1),
('bought', 1), ('bitter', 1), ('bit', 1), ('betty', 1), ('a', 1)]
</code></pre>

<p>thank you</p>

<p>You could use <code>list.sort</code> twice, since, according to <code>list.sort</code>'s docstring, it is in place:</p>

<pre><code>L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
</code></pre>

<p>So, to sort your list, you could do</p>

<pre><code>&gt;&gt;&gt; word_count.sort()
&gt;&gt;&gt; word_count.sort(key=lambda x: x[1], reverse=True)
&gt;&gt;&gt; print(word_count)
[('butter', 2), ('a', 1), ('betty', 1), ('bit', 1), ('bitter', 1),
 ('bought', 1), ('but', 1), ('of', 1), ('the', 1), ('was', 1)]
</code></pre>

<p>Just make sure to sort alphabetically first, then sort by the second key after that.</p>

python function sorting tuples