0
votes

I have seen similar posts about removing/filtering specific keys from a defaultdict, but all such options do not actually remove the null value-key pair but just create a new dictionary without them. Doing this, I would not be able to access the items of the cleaner defaultdict.

Here are the details of my problem:

I am trying to read multiple molecules from a list, such that I can access their various properties listed in their individual files. I am starting with indexing the atoms in each molecule; as keys and then appending the corresponding atom's atomic number, as values. To achieve this, I am using Python's defaultdict(list), to avoid KeyError.

element_list = [1, 6, 7, 8, 9, 15, 16, 17, 35, 53]
element_indices = defaultdict(list)
counter = 0
XX = [some array]
for atm in m.GetAtoms():
    aid = atm.GetIdx()
    do multiple things.......
    element_indices[atm.GetAtomicNum()].append(counter)
    counter +=  1

    for element in element_list:
        XX = np.array([X[i] for i in element_indices[element]])
        print name, ":", element, (element_indices[element])


printing result : 
11bpo : 1 [1, 4, 6, 8, 10, 12]
11bpo : 6 [0, 3, 5, 7, 9, 11]
11bpo : 7 [2, 13]
11bpo : 8 []
11bpo : 9 []
11bpo : 15 []
11bpo : 16 []
11bpo : 17 []
11bpo : 35 []
11bpo : 53 []

Since this molecule does not have any Cl, Br, I or O, the values for the same are []. I want to remove all such occurrences.

P.S: New to Python and this is my first post on StackOverflow! Thanks

1

1 Answers

0
votes

Just needed to do:

for element in element_list:
    if element in element_indices:

This will ignore all the key-value pairs which actually do not exist in each molecule.