title: Iterate over OrderedDict in Python
body: <p>I have the following <code>OrderedDict</code>:</p>
  
  <pre><code>OrderedDict([('r', 1), ('s', 1), ('a', 1), ('n', 1), ('y', 1)])
  </code></pre>
  
  <p>This actually presents a frequency of a letter in a word.</p>
  
  <p>In the first step - I would take the last two elements to create a union tuple like this;  </p>
  
  <pre><code> pair1 = list.popitem()
      pair2 = list.popitem()
      merge_list = (pair1[0],pair2[0])
      new_pair = {}
      new_pair[merge_list] = str(pair1[1] + pair2[1])
      list.update(new_pair);
  </code></pre>
  
  <p>This created for me the following OrderedList:</p>
  
  <pre><code>OrderedDict([('r', 1), ('s', 1), ('a', 1), (('y', 'n'), '2')])
  </code></pre>
  
  <p>I would like now to iterate over the elements, each time taking the last three and deciding based on the lower sum of the values what is the union object.</p>
  
  <p>For instance the above list will turn to;</p>
  
  <pre><code>OrderedDict([('r', 1), (('s', 'a'), '2'), (('y', 'n'), '2')])
  </code></pre>
  
  <p>but the above was:</p>
  
  <pre><code>OrderedDict([ ('r', 1), ('s', 2), ('a', 1), (('y', 'n'), '2')])
  </code></pre>
  
  <p>The result would be:</p>
  
  <pre><code>OrderedDict([('r', 1), ('s', 2), (('a','y', 'n'), '3')])
  </code></pre>
  
  <p>as I want the left ones to have the smaller value</p>
  
  <p>I tried to do it myself but doesn't understand how to iterate from end to beginning over an OrderedDict.</p>
  
  <p>How can I do it?</p>
  
  <p><strong>EDITED</strong>
  Answering the comment:</p>
  
  <p>I get a dictionary of frequency of a letter in a sentence:</p>
  
  <pre><code>{ 's':1, 'a':1, 'n':1, 'y': 1}
  </code></pre>
  
  <p>and need to create a huffman tree from it.</p>
  
  <p>for instance:</p>
  
  <pre><code>((s,a),(n,y))
  </code></pre>
  
  <p>I am using python 3.3</p>