I have created a Father ‘father_dict’ dictionary, in which I store list type data, but I am looking for that data to be stored as the key of another dictionary, (ultimately a kind of nested dictionary).
# SubKey Key
data_input = [[ 1, 'Col', 1, 'TWO', 450],
[ 2, 'Col', 1, 'TWO', 450],
[ 3, 'Col', 1, 'TWO', 450],
[ 4, 'Col', 2, 'TWO', 400],
[ 5, 'Col', 2, 'TWO', 400],
[ 6, 'Col', 2, 'TWO', 400],
[ 7, 'Col', 3, 'TWO', 300],
[ 8, 'Col', 3, 'TWO', 300],
[ 9, 'Col', 3, 'TWO', 300]]
cc_list = []
father_dict = {}
for i in range(len(data_input)):
if data_input[i][1] == 'Col':
cc_list.append(data_input[i][2]) #--> key list
flat_list = list(set(cc_list)) #--> Key flat
for j in flat_list:
father_dict[j] = []
for i in range(len(data_input)):
if data_input[i][1] == 'Col':
key = data_input[i][2]
father_dict[key].append(data_input[i][0])
print('\n', 'Father Dict ')
print(father_dict)
The problem is that when replacing 'father_dict [j] = [] with {}', I get an Attribute error because in dictionaries the append method cannot be applied, I have tried some things like matching 'father_dict [key] = data_input [ i] [0] ', but it doesn't work either.
What I'm looking for is that when printing on screen it returns this:
Father Dict
{1: {1:, 2:, 3:}, 2: {4:, 5:, 6:}, 3: {7:, 8:, 9:}}
Is there any method that works for these cases, thanks?