title: Sorting dictionary using operator.itemgetter
body: <p><a href="https://stackoverflow.com/questions/4690094/sorting-dictionary-keys-based-on-their-values">A question was asked here on SO</a>, a few minutes ago, on sorting dictionary keys based on their values. </p>
  
  <p>I just read about the <code>operator.itemgetter</code> method of sorting a few days back and decided to try that, but it doesn't seem to be working.</p>
  
  <p>Not that I have any problems with the answers presented to the questions, I just wanted to try this with <code>operator.itemgetter</code>. </p>
  
  <p>So the dict was:</p>
  
  <pre><code>&gt;&gt;&gt; mydict = { 'a1': ['g',6],
             'a2': ['e',2],
             'a3': ['h',3],
             'a4': ['s',2],
             'a5': ['j',9],
             'a6': ['y',7] }
  </code></pre>
  
  <p>I tried this:</p>
  
  <pre><code>&gt;&gt;&gt; l = sorted(mydict.itervalues(), key=operator.itemgetter(1))
  &gt;&gt;&gt; l
  [['e', 2], ['s', 2], ['h', 3], ['g', 6], ['y', 7], ['j', 9]]
  </code></pre>
  
  <p>And this works as I want it to. However, since I don't have the complete dictionary (<code>mydict.itervalues()</code>), I tried this:</p>
  
  <pre><code>&gt;&gt;&gt; complete = sorted(mydict.iteritems(), key=operator.itemgetter(2))
  </code></pre>
  
  <p>This doesn't work (as I expected it to).</p>
  
  <p>So how do I sort the dict using <code>operator.itemgetter</code> and call <code>itemgetter</code> on the nested key - value pair. </p>