I'm trying to do something when a key exists in a sub-dictionary, and do something else when the key doesn't exist (although here for demonstration I let it print 0).
"dict_main" is a dictionary of lists of dictionaries..and at the [j] level it is a dictionary, above it is a list. Which is one the len() function before the inner loop starts measures how many dictionaries exist in the list.
The code seems solid, yet i'm still getting KeyError 0. After some searching it seems the error occurs when key doesn't exist, but this whole code is written with the If condition to handle it?? (the if condition to see if the key exists should be fine, as i try using the "if 'key' in dict" method as well, and the result is the same.)
Standing by. Please help!! Here is the code:
for i in codeset_of_interest:
len_root_code = len(dict_main['pool']['codeSets']['group'][i]['codes']['code'])
for j in range(len_root_code):
if dict_main['pool']['codeSets']['group'][i]['codes']['code'][j].get('name'):
list_keyword.append(dict_main['pool']['codeSets']['group'][i]['codes']['Code'][j]['name'])
else:
print('0')
Thank you!
Aucun commentaire:
Enregistrer un commentaire