Update dictionary keys to be in ascending order?
<p>I have a dictionary:</p>

<pre><code>A = {'cat': {0: {'variable_1': 'xxx', 'variable_2': 'yyy'},
             1: {'variable_1': 'ttt', 'variable_2': 'kkk'}},
     'dog': {0: {'variable_1': 'xxx', 'variable_2': 'ppp'},
             1: {'variable_1': 'qqq', 'variable_2': 'www'}},
     'fox': {0: {'variable_1': 'xxx', 'variable_2': 'zzz'},
             1: {'variable_1': 'yyy', 'variable_2': 'uuu'},
             3: {'variable_1': 'ccc', 'variable_2': 'jjj'}}}
</code></pre>

<p>I want to make the second level key automated ascend
like this:</p>

<pre><code>{'cat': {0: {'variable_1': 'xxx', 'variable_2': 'yyy'},
         1: {'variable_1': 'ttt', 'variable_2': 'kkk'}},
 'dog': {2: {'variable_1': 'xxx', 'variable_2': 'ppp'},
         3: {'variable_1': 'qqq', 'variable_2': 'www'}},
 'fox': {4: {'variable_1': 'xxx', 'variable_2': 'zzz'},
         5: {'variable_1': 'yyy', 'variable_2': 'uuu'},
         6: {'variable_1': 'ccc', 'variable_2': 'jjj'}}}
</code></pre>

<p>Regular dictionaries are unordered, so you will need to use <a href="https://docs.python.org/3/library/collections.html#collections.OrderedDict" rel="nofollow"><em>OrderedDict</em></a>.</p>

<p>A global <em>counter</em> variable can keep track of the total number of entries.</p>

<p>The <a href="https://docs.python.org/3/library/functions.html#sorted" rel="nofollow"><em>sorted</em></a> function will take a list of key/value tuples and sort them according to the key.</p>

<pre><code>&gt;&gt;&gt; from collections import OrderedDict
&gt;&gt;&gt; A = {'cat': {0: {'variable_1': 'xxx', 'variable_2': 'yyy'},
                 1: {'variable_1': 'ttt', 'variable_2': 'kkk'}},
         'dog': {0: {'variable_1': 'xxx', 'variable_2': 'ppp'},
                 1: {'variable_1': 'qqq', 'variable_2': 'www'}},
         'fox': {0: {'variable_1': 'xxx', 'variable_2': 'zzz'},
                 1: {'variable_1': 'yyy', 'variable_2': 'uuu'},
                 3: {'variable_1': 'ccc', 'variable_2': 'jjj'}}}

&gt;&gt;&gt; OA = OrderedDict()
&gt;&gt;&gt; count = 0
&gt;&gt;&gt; for animal, info in sorted(A.items()):
        OA[animal] = OrderedDict()
        for i, variables in sorted(info.items()):
            OA[animal][count] = variables
            count += 1

&gt;&gt;&gt; OA
OrderedDict([
   ('cat', OrderedDict([(0, {'variable_2': 'yyy', 'variable_1': 'xxx'}), 
                        (1, {'variable_2': 'kkk', 'variable_1': 'ttt'})])),
   ('dog', OrderedDict([(2, {'variable_2': 'ppp', 'variable_1': 'xxx'}), 
                        (3, {'variable_2': 'www', 'variable_1': 'qqq'})])),
   ('fox', OrderedDict([(4, {'variable_2': 'zzz', 'variable_1': 'xxx'}),
                        (5, {'variable_2': 'uuu', 'variable_1': 'yyy'}),
                        (6, {'variable_2': 'jjj', 'variable_1': 'ccc'})]))
])
</code></pre>

<p>If needed you can sort the innermost variables and stored them in an OrderedDict as well.</p>

python dictionary key value auto-update