Home > Software engineering >  how to retrieve dictionary's unquoted values
how to retrieve dictionary's unquoted values

Time:10-14

{'CMC Threat Intelligence': {'detected': False, 'result': 'clean site'}, 'Snort IP sample list': {'detected': True, 'result': 'clean site'}, '0xSI_f33d': {'detected': False, 'result': 'unrated site'}}

This is a sample result (value) of a dictionary's key. If I use:

for i in result[key]:
    print(i)

it will return:

CMC Threat Intelligence
Snort IP sample list
0xSI_f33d

But the question is how would I retrieve the keys or values of the dictionaries inside, which are the unquoted values of the quoted keys of the main result.

For example, how would I return the values of those where the 'detected' = True Or the value of 'result' where the value is 'clean site'. Thanks

CodePudding user response:

Use result[key].items() to loop over both the keys and values of the dictionary. Then you can access values from the nested dictionary.

for name, d in result[key].items():
    print(f'{name}: {d['result']}')

This will print:

CMC Threat Intelligence: clean site
Snort IP sample list: clean site
0xSI_f33d: unrated site

CodePudding user response:

by default, dicts in python return their key for iteration. code below will solve your problem:

for key,val in dict.items():
    if val['detected'] or val['result'] == 'clean site':
        ..do something..
  • Related