I am trying to create a dictionary from the values in the name_num
dictionary where the length of the list is the new key and the name_num
dictionary key and value are the value. So:
name_num = {"Bill": [1,2,3,4], "Bob":[3,4,2], "Mary": [5, 1], "Jim":[6,17,4], "Kim": [21,54,35]}
I want to create the following dictionary:
new_dict = {4:{"Bill": [1,2,3,4]}, 3:{"Bob":[3,4,2], "Jim":[6,17,4], "Kim": [21,54,35]}, 2:{"Mary": [5, 1]}}
I've tried many variations, but this code gets me the closest:
for mykey in name_num:
new_dict[len(name_num[mykey])] = {mykey: name_num[mykey]}
Output:
new_dict = {4:{"Bill": [1,2,3,4]}, 3:{"Jim":[6,17,4]}, 2:{"Mary": [5, 1]}}
I know I need to loop through the code somehow so I can add the other values to key 3.