this is the dict I got now:
dict1 = {1: {7: [6], 1: [7]}, 2: {3: [1, 6], 2: [2, 7, 5]}}
And I would like to order subdictionary by keys:
dict2 = {1: {1: [7], 7: [6]}, 2: {2: [2, 7, 5], 3: [1, 6]}}
I tried some methods, but it didn't work...Like this one. Please help me.
print(sorted(dict1.items(), key=lambda x: x[1][1]))
CodePudding user response:
As long as you use sufficiently high version of Python3 you can do:
dict1 = {1: {7: [6], 1: [7]}, 2: {3: [1, 6], 2: [2, 7, 5]}}
dict2 = {k: dict(sorted(v.items())) for k, v in dict1.items()}
print(dict2)
Prints:
{1: {1: [7], 7: [6]}, 2: {2: [2, 7, 5], 3: [1, 6]}}