sorting nested keys in a dictionary

Viewed 55
dict_1 = {
    (40, 615): {1: ["03/02/2013"]},
    (40, 558): {1: ["03/20/2013"]},
    (40, 501): {1: ["04/03/2013"]},
    (40, 342): {1: ["04/04/2013"]},
    (40, 248): {1: ["04/08/2013"]},
    (40, 88): {1: ["04/12/2013"]},
    (40, 584): {8: ["06/21/2013"]},
    (40, 480): {2: ["04/12/2013"]},
    (40, 321): {2: ["04/12/2013"]},
    (40, 121): {2: ["04/13/2013"]},
    (40, 563): {3: ["04/15/2013"]},
    (40, 404): {3: ["04/24/2013"]},
    (40, 245): {3: ["04/26/2013"]},
    (40, 141): {3: ["04/26/2013"]},
    (40, 578): {4: ["04/29/2013"]},
    (40, 474): {4: ["04/30/2013"]},
    (40, 370): {4: ["04/30/2013"]},
    (40, 201): {7: ["06/20/2013"]},
    (64, 643): {5: ["05/03/2013"]},
    (40, 484): {5: ["05/08/2013"]},
    (40, 287): {5: ["05/31/2013"]},
    (40, 171): {5: ["05/31/2013"]},
    (40, 660): {6: ["06/03/2013"]},
    (40, 544): {6: ["06/04/2013"]},
    (40, 440): {6: ["06/12/2013"]},
    (40, 281): {6: ["06/12/2013"]},
    (40, 177): {6: ["06/12/2013"]},
    (40, 619): {7: ["06/13/2013"]},
    (40, 515): {7: ["06/14/2013"]},
    (40, 411): {7: ["06/18/2013"]},
    (40, 295): {7: ["06/19/2013"]},
    (40, 97): {7: ["06/21/2013"]},
}

I want to sort this dictionary on nested keys.

Here is what I tried

def sort_dictionary(*new_dict):
     for unpack in new_dict:
         for key, value in sorted(unpack .items()):
             return key,':',value

res = sorted(dict_1 .items(), key = lambda x: sort_dictionary(x[1]))

Of course, it is not giving me what I am expecting, please tell me if there is an easier way?

2 Answers

Try it online!

def sort_dict(d):
    return dict(sorted(d.items(), key = lambda x: tuple(x[1].keys())[0]))

dict_1 = {
    (40, 615): {1: ["03/02/2013"]},
    (40, 558): {1: ["03/20/2013"]},
    (40, 501): {1: ["04/03/2013"]},
    (40, 342): {1: ["04/04/2013"]},
    (40, 248): {1: ["04/08/2013"]},
    (40, 88): {1: ["04/12/2013"]},
    (40, 584): {8: ["06/21/2013"]},
    (40, 480): {2: ["04/12/2013"]},
    (40, 321): {2: ["04/12/2013"]},
    (40, 121): {2: ["04/13/2013"]},
    (40, 563): {3: ["04/15/2013"]},
    (40, 404): {3: ["04/24/2013"]},
    (40, 245): {3: ["04/26/2013"]},
    (40, 141): {3: ["04/26/2013"]},
    (40, 578): {4: ["04/29/2013"]},
    (40, 474): {4: ["04/30/2013"]},
    (40, 370): {4: ["04/30/2013"]},
    (40, 201): {7: ["06/20/2013"]},
    (64, 643): {5: ["05/03/2013"]},
    (40, 484): {5: ["05/08/2013"]},
    (40, 287): {5: ["05/31/2013"]},
    (40, 171): {5: ["05/31/2013"]},
    (40, 660): {6: ["06/03/2013"]},
    (40, 544): {6: ["06/04/2013"]},
    (40, 440): {6: ["06/12/2013"]},
    (40, 281): {6: ["06/12/2013"]},
    (40, 177): {6: ["06/12/2013"]},
    (40, 619): {7: ["06/13/2013"]},
    (40, 515): {7: ["06/14/2013"]},
    (40, 411): {7: ["06/18/2013"]},
    (40, 295): {7: ["06/19/2013"]},
    (40, 97): {7: ["06/21/2013"]},
}

print(sort_dict(dict_1))

Output:

{
    (40, 615): {1: ["03/02/2013"]},
    (40, 558): {1: ["03/20/2013"]},
    (40, 501): {1: ["04/03/2013"]},
    (40, 342): {1: ["04/04/2013"]},
    (40, 248): {1: ["04/08/2013"]},
    (40, 88): {1: ["04/12/2013"]},
    (40, 480): {2: ["04/12/2013"]},
    (40, 321): {2: ["04/12/2013"]},
    (40, 121): {2: ["04/13/2013"]},
    (40, 563): {3: ["04/15/2013"]},
    (40, 404): {3: ["04/24/2013"]},
    (40, 245): {3: ["04/26/2013"]},
    (40, 141): {3: ["04/26/2013"]},
    (40, 578): {4: ["04/29/2013"]},
    (40, 474): {4: ["04/30/2013"]},
    (40, 370): {4: ["04/30/2013"]},
    (64, 643): {5: ["05/03/2013"]},
    (40, 484): {5: ["05/08/2013"]},
    (40, 287): {5: ["05/31/2013"]},
    (40, 171): {5: ["05/31/2013"]},
    (40, 660): {6: ["06/03/2013"]},
    (40, 544): {6: ["06/04/2013"]},
    (40, 440): {6: ["06/12/2013"]},
    (40, 281): {6: ["06/12/2013"]},
    (40, 177): {6: ["06/12/2013"]},
    (40, 201): {7: ["06/20/2013"]},
    (40, 619): {7: ["06/13/2013"]},
    (40, 515): {7: ["06/14/2013"]},
    (40, 411): {7: ["06/18/2013"]},
    (40, 295): {7: ["06/19/2013"]},
    (40, 97): {7: ["06/21/2013"]},
    (40, 584): {8: ["06/21/2013"]},
}
def sort_dict(d):
    return sorted(d.items(), key = lambda x: tuple(x[1].keys())[0]) 

sorted_dictionary = sort_dict(dict_1)

This code works great! But then I had to write some extra lines of code to convert sorted_dictionary from a list of tuples to a nested dictionary.

new_dict = {} 
for item in sorted_dictionary : 
    new_dict[item[0]] = item[1] 

print(new_dict)

{(40, 615): {1: ['03/02/2013']},
 (40, 558): {1: ['03/20/2013']},
 (40, 501): {1: ['04/03/2013']},
 (40, 342): {1: ['04/04/2013']},
 (40, 248): {1: ['04/08/2013']},
 (40, 88): {1: ['04/12/2013']},
 (40, 480): {2: ['04/12/2013']},
 (40, 321): {2: ['04/12/2013']},
 (40, 121): {2: ['04/13/2013']},
 (40, 563): {3: ['04/15/2013']},
 (40, 404): {3: ['04/24/2013']},
 (40, 245): {3: ['04/26/2013']},
 (40, 141): {3: ['04/26/2013']},
 (40, 578): {4: ['04/29/2013']},
 (40, 474): {4: ['04/30/2013']},
 (40, 370): {4: ['04/30/2013']},
 (64, 643): {5: ['05/03/2013']},
 (40, 484): {5: ['05/08/2013']},
 (40, 287): {5: ['05/31/2013']},
 (40, 171): {5: ['05/31/2013']},
 (40, 660): {6: ['06/03/2013']},
 (40, 544): {6: ['06/04/2013']},
 (40, 440): {6: ['06/12/2013']},
 (40, 281): {6: ['06/12/2013']},
 (40, 177): {6: ['06/12/2013']},
 (40, 201): {7: ['06/20/2013']},
 (40, 619): {7: ['06/13/2013']},
 (40, 515): {7: ['06/14/2013']},
 (40, 411): {7: ['06/18/2013']},
 (40, 295): {7: ['06/19/2013']},
 (40, 97): {7: ['06/21/2013']},
 (40, 584): {8: ['06/21/2013']}}

Is there a better way without the extra lines of code?

Related