Python: sorting the lists of dictionary based on the indices of one list
<p>So I have dict like the following:</p>

<pre><code>dict1 = {1: [-1, 1, 2, 4, 3], 2: [11, 10, 9, 8, 7]}
</code></pre>

<p>How can I sort the lists in <code>dict1</code> based on the indices that would sort one of the lists. Such as the sorted indices of list </p>

<pre><code>[11, 10, 9, 8, 7]
</code></pre>

<p>which is</p>

<pre><code>[4, 3, 2, 1, 0]
</code></pre>

<p>So the new <code>dict1</code> will be:</p>

<pre><code>dict1 = {1: [3, 4, 2, 1, -1], 2: [7, 8, 9, 10, 11]}
</code></pre>

<p>I have tried the code like this:</p>

<pre><code>sorted(dict1.items(), key=lambda t: t[1])
</code></pre>

<p>But still cannot figure it out? I can use a for loop to do this work but I believe there exist a more pythonic way. Any idea?</p>

<p>Using <a href="https://docs.python.org/2/library/operator.html#operator.itemgetter" rel="nofollow"><code>itemgetter</code></a> in a <a href="https://www.python.org/dev/peps/pep-0274/" rel="nofollow">dictionary comprehension</a></p>

<pre><code>&gt;&gt;&gt; from operator import itemgetter
&gt;&gt;&gt; order = [4, 3, 2, 1, 0]
&gt;&gt;&gt; key = itemgetter(*order)
&gt;&gt;&gt; {k: key(dict1[k]) for k in dict1}
{1: (3, 4, 2, 1, -1), 2: (7, 8, 9, 10, 11)}
</code></pre>

python list dictionary