Parsing dict with nested dict of lists

PythonDude

I am trying to parse the following dict

{'IsTruncated': False,
    'MaxItems': '100',
    'ResourceRecordSets': [{'Name': 'test.com.',
        {'Name': '1.test.com.',
        'ResourceRecords': [{'Value': '10.0.0.1'}],
        {'Name': '2.test.com.',
        'ResourceRecords': [{'Value': '10.0.0.2'}],
}

The output I am looking for is:

1.test.com    10.0.0.1
2.test.com    10.0.0.2

I have tried:

for resource in response['ResourceRecordSets']:
    print("{}  {}".format(resource['Name'], resource['ResourceRecords'] ))

and

for resource in response['ResourceRecordSets']:
    print("{}  {}".format(resource['Name'], resource['ResourceRecords'][0] ))

Is there a simple way to access this dict key/values within the nested list?

Rob Truxal

Assuming that your dictionary should look like this:

{'IsTruncated': False,
'MaxItems': '100',
'ResourceRecordSets': [
    {'Name': 'test.com.', 
    'ResourceRecords' : [{'Value' : '<mising ip addr>'}]},
    {'Name': '1.test.com.',
    'ResourceRecords': [{'Value': '10.0.0.1'}]},
    {'Name': '2.test.com.',
    'ResourceRecords': [{'Value': '10.0.0.2'}]},
    ]

Try this:

for resource in response['ResourceRecordSets']:
    print("{}  {}".format(resource['Name'], resource['ResourceRecords'][0]['Value']))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related