title: sorting key-value pairs in python
body: <p>I am working on a small program as written below</p>
  
  <pre><code>"""Count words."""
      # TODO: Count the number of occurences of each word in s
  
      # TODO: Sort the occurences in descending order (alphabetically in case of ties)
  
      # TODO: Return the top n words as a list of tuples (&lt;word&gt;, &lt;count&gt;)    
  from operator import itemgetter
  
  def count_words(s, n):
      """Return the n most frequently occuring words in s."""
  
      t1=[]
      t2=[]
      temp={}
      top_n={}
      words=s.split()
      for word in words:
          if word not in temp:
              t1.append(word)
              temp[word]=1
          else:
              temp[word]+=1
  
      t1 = sorted(temp,key=temp.get,reverse=True) # to get sorted keys
      t2 = sorted(temp.values(),reverse=True) # to get sorted values
      top_n = dict(zip(t1,t2))
      print top_n
  
      return 
  
  
  def test_run():
      """Test count_words() with some inputs."""
      count_words("cat bat mat cat bat cat", 3)
      count_words("betty bought a bit of butter but the butter was bitter", 3)
  
  
  if __name__ == '__main__':
      test_run()
  </code></pre>
  
  <p>I am just trying to sort the key-value pair. I have below questions :</p>
  
  <ol>
  <li>In the above program when I print merge of two sorted list its showing me only the unsorted merger</li>
  <li>How to get the sorted key value pair by python function the current fxn which I am using it wither returns the keys or values. Can we get both somehow?</li>
  </ol>