Search code examples
pythondictionarykeyauto-update

Update dictionary keys to be in ascending order?


I have a dictionary:

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'}}}

I want to make the second level key automated ascend like this:

{'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'}}}

Solution

  • Regular dictionaries are unordered, so you will need to use OrderedDict.

    A global counter variable can keep track of the total number of entries.

    The sorted function will take a list of key/value tuples and sort them according to the key.

    >>> from collections import OrderedDict
    >>> 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'}}}
    
    >>> OA = OrderedDict()
    >>> count = 0
    >>> for animal, info in sorted(A.items()):
            OA[animal] = OrderedDict()
            for i, variables in sorted(info.items()):
                OA[animal][count] = variables
                count += 1
    
    >>> 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'})]))
    ])
    

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