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>As martineau mentioned, If you want to keep a data set sorted, you'll want to use a collection/data type that preserves order. For a record like this, you can use tuples or namedtuples. These will allow you to keep data sorted, and having them in a list allows them compatibility with built in functions that allow to you insert future data into an sorted set.</p>

python dictionary key value auto-update