Sorting the tuples of tuples in alphabetical order
<p>I have a list of tuples<br>
<code>[((A,B),2),((C,B),3)]</code>
Which I need to sort as</p>

<pre><code>[((A,B),2),((B,C),3)]
</code></pre>

<p>I need all the tuples to be in alphabetical order using the <code>sorted()</code> method. I have used <code>key = lambda x : x[0]</code> but doesn't work. Any ideas?</p>

<p>You can use <a href="https://docs.python.org/3/howto/sorting.html" rel="nofollow"><code>sorted</code></a> within <a href="http://www.secnetix.de/olli/Python/list_comprehensions.hawk" rel="nofollow">list comprehension</a>:</p>

<pre><code>In [3]: tups = [(('A','B'),2),(('C','B'),3)]

In [4]: [(sorted(t[0]), t[1]) for t in tups]
Out[4]: [(['A', 'B'], 2), (['B', 'C'], 3)]
</code></pre>

python python-3.x