Sorting the keys of a dictionary based on different values
<p>I have a dictionary:</p>

<pre><code>d = {"A":{"a":1, "b":2, "c":3}, "B":{"a":5, "b":6, "c":7}, "C":{"a":4, "b":6, "c":7}}
</code></pre>

<p>I want to sort the keys "A", "B" and "C" in a list, first on the basis of numerical values of "a", then if some tie occurs on the basis of numerical values of "b" and so on. </p>

<p>How can I do it?</p>

<pre><code>&gt;&gt;&gt; d = {"A":{"a":1, "b":2, "c":3}, "B":{"a":5, "b":6, "c":7}, "C":{"a":4, "b":6, "c":7}}
&gt;&gt;&gt; 
&gt;&gt;&gt; d.items()
[('A', {'a': 1, 'c': 3, 'b': 2}), ('C', {'a': 4, 'c': 7, 'b': 6}), ('B', {'a': 5, 'c': 7, 'b': 6})]
&gt;&gt;&gt; sorted(d.items(), key=lambda x: [y[1] for y in sorted(x[1].items())])
[('A', {'a': 1, 'c': 3, 'b': 2}), ('C', {'a': 4, 'c': 7, 'b': 6}), ('B', {'a': 5, 'c': 7, 'b': 6})]
</code></pre>

python sorting dictionary ranking