So basically since keys need to be sorted in the nested dict, go through the dictionary and transfer the data to a new dictionary.
>>> new_d = OrderedDict()
>>> for key,val in d.items(): #go through the dictionary
new_d[key] = OrderedDict(sorted(val.items())) #sort according to keys
#driver values
IN : d
{
'test' :{ 0:[1,3] ,
1:[5,6]
},
'test2':{ 7:[9],
3:[4,6]
}
}
OUT : new_d
OrderedDict([('test', {0: [1, 3], 1: [5, 6]}), ('test2', {3: [4, 6], 7: [9]})])
Edit : As the OP wants the dictionary's initial keys (EX : test3:{ ... } , test2:{ ... } ) to be sorted too, the below changes need to be done :
>>> initial_sort = OrderedDict(sorted(d.items()))
>>> inital_sort
OrderedDict([('test2', {7: [9], 3: [4, 6]}), ('test3', {0: [1, 3], 1: [5, 6]})])
>>> new_d = OrderedDict()
>>> for key,val in initial_sort.items(): #go through the first key sorted dictionary
new_d[key] = OrderedDict(sorted(val.items()))
#driver values
IN : d = {'test3': {0: [1, 3], 1: [5, 6]}, 'test2': {7: [9], 3: [4, 6]}}
OUT : new_d = OrderedDict([('test2', OrderedDict([(3, [4, 6]), (7, [9])])), ('test3', OrderedDict([(0, [1, 3]), (1, [5, 6])]))])
test2related totesthere?