Anderson Soares
Anderson Soares

Reputation: 76

Not able to access nested Python dictionary with list inside of it

I have the nested dictionary below:

facter_networking: {"domain": "mylab.com", "fqdn": "mylab.com", "hostname": "mylab", "interfaces": {"ens192": {"bindings": [{"address": "20.9.8.1", "netmask": "255.255.255.221", "network": "20.33.50.62"}], "ip": "20.67.83.48", "mac": "00:00:06:0:e0:d6", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48"}, "ens224": {"bindings": [{"address": "20.67.83.48", "netmask": "20.67.83.48", "network": "20.67.83.48"}], "ip": "20.67.83.48", "mac": "20.67.83.48", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48"}, "lo": {"bindings": [{"address": "127.0.0.1", "netmask": "255.0.0.0", "network": "127.0.0.0"}], "ip": "127.0.0.1", "mtu": 65536, "netmask": "255.0.0.0", "network": "127.0.0.0"}}, "ip": "20.67.83.48", "mac": "00:00:00:9d:8f:d7", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48", "primary": "ens224"}

I can read mostly of the Keys values, but some of them I cannot.

for example:

print(myfile['facter_networking']['domain']) - it works.
print(myfile['facter_networking']['fqdn']) - it works.
print(myfile['facter_networking']['hostname'])    - it works.

However if I do.

print(myfile['facter_networking']['interfaces']['ens192']['bindings']['address']) - It doens't work.

On the other hand if I do:

print(myfile['facter_networking']['interfaces']

I get as result:

{"ens192": {"bindings": [{"address": "20.9.8.1", "netmask": "255.255.255.221", "network": "20.33.50.62"}], "ip": "20.67.83.48", "mac": "00:00:06:0:e0:d6", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48"}, "ens224": {"bindings": [{"address": "20.67.83.48", "netmask": "20.67.83.48", "network": "20.67.83.48"}], "ip": "20.67.83.48", "mac": "20.67.83.48", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48"}, "lo": {"bindings": [{"address": "127.0.0.1", "netmask": "255.0.0.0", "network": "127.0.0.0"}], "ip": "127.0.0.1", "mtu": 65536, "netmask": "255.0.0.0", "network": "127.0.0.0"}}, "ip": "20.67.83.48", "mac": "00:00:00:9d:8f:d7", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48", "primary": "ens224"}

Any idea how can I access these values above?

Upvotes: 0

Views: 146

Answers (4)

klv0000
klv0000

Reputation: 174

d = {
    'facter_networking': {
        "domain": "mylab.com",
        "fqdn": "mylab.com",
        "hostname": "mylab", 
        "interfaces": {
                        "ens192": 
                                {
                                "bindings": [{"address": "20.9.8.1", "netmask": "255.255.255.221", "network": "20.33.50.62"}],
                                "ip": "20.67.83.48", "mac": "00:00:06:0:e0:d6", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48"}, 
                                "ens224": {"bindings": [{"address": "20.67.83.48", "netmask": "20.67.83.48", "network": "20.67.83.48"}], 
                                "ip": "20.67.83.48", "mac": "20.67.83.48", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48"}, 
                                "lo": {"bindings": [{"address": "127.0.0.1", "netmask": "255.0.0.0", "network": "127.0.0.0"}],
                                "ip": "127.0.0.1", "mtu": 65536, "netmask": "255.0.0.0", "network": "127.0.0.0"}},
        "ip": "20.67.83.48", "mac": "00:00:00:9d:8f:d7",
        "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48", "primary": "ens224"
    }
}

print(d['facter_networking']['interfaces']['ens192']['bindings'][0]['address'])

Output:

20.9.8.1

Upvotes: 0

A-Z
A-Z

Reputation: 11

Quick fix: Your problem lies with your access to the last nested dictionary, "address". Since it is nested in a list before being a dictionary, you should get it like so:

myfile['facter_networking']['interfaces']['ens192']['bindings'][insert desired index here]['address']

Much better fix: However, you currently have a huge design problem. Nesting dicts in dicts in lists in dicts in dicts is a nesting hell, it prevents you from coding efficiently by making it much harder to debug whatever is happening. Luckily, python is an object oriented language! You can replace most of your dictionaries with dedicated objects, effectively managing your code better, and making it less likely for future bugs like this one to occur.

Upvotes: 1

bigbounty
bigbounty

Reputation: 17368

As said in the comment section, {"bindings": [{"address" is a list. And as always if you try to access an empty list using an index, it will throw exception. Instead loop through it, so that you will get empty list as result in case the list is empty

In [66]: [i["address"] for i in myfile['facter_networking']['interfaces']['ens192']['bindings']]
Out[66]: ['20.9.8.1']

Upvotes: 1

Nathan
Nathan

Reputation: 3648

The problem is that you try to access a list like a dict:

...{"bindings": [{"address": "20.9.8.1...

To acces the address you need

....["bindings"][0]["address"]

Upvotes: 0

Related Questions