Reordering Dictionary based on specific order
<p>I have a dictionary in the below key, value format:</p>

<pre><code>PairDict= {(19, 6): 13, (2, 29): 10, (38, 8): 20, (38, 5): 5} 
</code></pre>

<p>The key is a tuple with two elements, and the value represent number of cycle.</p>

<p>I would need to reorder the dictionary into a list based on a given sort order of the first element of the tuple e.g</p>

<pre><code>SortOrder= [(38, 25), (19, 13), (2, 10)]
</code></pre>

<p>Where, the first element of the tuple in SortOrder above represent the first element of the tuple key in PairDict. The second element of the tuple in SortOrder represent the sum of number of cycle for the first element in PairDict</p>

<p>If the PairDict has more than one tuple with the same first element, its sort order could in anyway, e.g.
38 need to be sorted first, in PictDict there are two tuples starting with 38, that is <code>(38, 8)</code> and <code>(38, 5)</code>. Its order can be either <code>[(38, 8): 20, (38, 5): 5]</code> or <code>[(38, 5): 5, (38, 8): 20]</code>.</p>

<p>The expected output of the ordering PairDict based on SortOrder can be,</p>

<pre><code>SortedPairDict= [((38, 8), 20), ((38, 5), 5), ((19, 6), 13), ((2, 29), 10)], 
</code></pre>

<p>or</p>

<pre><code>SortedPairDict= [((38, 5), 5), ((38, 8), 20), ((19, 6), 13), ((2, 29), 10)],
</code></pre>

<p>Tried to search, the closest example I could get is,</p>

<p>Refer: <a href="http://stackoverflow.com/questions/3301406/reordering-list-of-dicts-arbitrarily-in-python">reordering list of dicts arbitrarily in python</a></p>

<p>But having problem to realize my code with that example as my key is a tuple. </p>

<p>Can you please point me if there is any examples I can read on this or guide me how is the best way can handle this.</p>

<p>Appreciate your help. Merry Christmas :-)</p>

<p>Thanks.</p>

<p><strong>Update : timeit Analysis Amber and Ashwini's Solution</strong></p>

<pre><code>PairDict= {(19, 8): 13, (2, 29): 10, (38, 8): 20, (2, 18): 10, (43, 8): 20, (5, 13): 15, (6, 21): 9, (7, 25): 11, (8, 19): 15, (44, 4): 20, (0, 10): 9, (0, 36): 9, (21, 6): 19, (30, 4): 17, (24, 0): 11, (3, 14): 21, (6, 12): 9, (7, 20): 11, (20, 7): 10, (5, 23): 15, (11, 5): 15, (37, 6): 13, (34, 8): 20, (18, 2): 10, (0, 24): 9, (12, 6): 13, (8, 38): 15, (39, 5): 18, (42, 5): 17, (26, 7): 22, (10, 0): 9, (31, 0): 14, (3, 27): 21, (6, 37): 9, (36, 0): 17, (0, 33): 9, (41, 0): 18, (5, 39): 15, (5, 42): 15, (6, 40): 9, (5, 11): 15, (9, 3): 21, (25, 7): 13, (7, 28): 11, (3, 9): 21, (29, 2): 4, (32, 4): 22, (0, 41): 9, (40, 6): 10, (28, 7): 20, (6, 15): 9, (17, 4): 23, (23, 5): 18, (16, 5): 23, (8, 34): 15, (5, 16): 15, (8, 22): 15, (8, 43): 15, (4, 17): 19, (13, 5): 23, (4, 30): 19, (7, 26): 11, (15, 6): 13, (4, 44): 19, (22, 8): 18, (0, 31): 9, (35, 5): 23, (14, 3): 20, (33, 0): 12, (5, 35): 15, (27, 3): 16, (4, 32): 19}

SortOrder= [(5, 105), (4, 76), (8, 75), (3, 63), (0, 54), (6, 45), (7, 44), (13, 23), (16, 23), (17, 23), (35, 23), (26, 22), (32, 22), (9, 21), (2, 20), (14, 20), (28, 20), (34, 20), (38, 20), (43, 20), (44, 20), (21, 19), (22, 18), (23, 18), (39, 18), (41, 18), (30, 17), (36, 17), (42, 17), (27, 16), (11, 15), (31, 14), (12, 13), (15, 13), (19, 13), (25, 13), (37, 13), (33, 12), (24, 11), (18, 10), (20, 10), (40, 10), (10, 9), (29, 4)]
</code></pre>

<p>In [5]: %timeit amber()
10000 loops, best of 3: 71.1 us per loop</p>

<p>In [6]: %timeit ashwc()
1000 loops, best of 3: 753 us per loop</p>
