2

I have several nested dictionaries within lists, and I need to verify if a specific path exist e.g.

dict1['layer1']['layer2'][0]['layer3']

How can I check with an IF statement if the path is valid?

I was thinking to

if dict1['layer1']['layer2'][0]['layer3'] :

but it doesn't work

Luigi T.
  • 457
  • 2
  • 10
  • 20
  • 2
    One way would be to [ask for forgiveness](http://stackoverflow.com/questions/12265451/ask-forgiveness-not-permission-explain) rather than testing for its existence. – roganjosh Apr 17 '17 at 19:54
  • So, you `try` to follow that path and assume it always exists, `except` when it doesn't. – ForceBru Apr 17 '17 at 19:55
  • I try to assign the value in the path in a variable, but it fails returning an error (KeyError: 'media' ) – Luigi T. Apr 17 '17 at 21:11

4 Answers4

4

Here's the explicit short code with try/except:

try:
    dict1['layer1']['layer2'][0]['layer3']
except KeyError:
    present = False
else:
    present = True

if present: 
    ...

To get the element:

try:
    obj = dict1['layer1']['layer2'][0]['layer3']
except KeyError:
    obj = None  # or whatever
Kirill Bulygin
  • 3,434
  • 1
  • 16
  • 21
0

As far as I know, you've to go step by step, i.e.:

if 'layer1' in dict1:
   if 'layer2' in dict1['layer1']

ans so on...

Pedro Lobito
  • 85,689
  • 29
  • 230
  • 253
0

If you don't want to go the try/except route, you could whip up a quick method to do this:

def check_dict_path(d, *indices):
    sentinel = object()
    for index in indices:
        d = d.get(index, sentinel)
        if d is sentinel:
            return False
    return True


test_dict = {1: {'blah': {'blob': 4}}}

print check_dict_path(test_dict, 1, 'blah', 'blob') # True
print check_dict_path(test_dict, 1, 'blah', 'rob') # False

This might be redundant if you're also trying to retrieve the object at that location (rather than just verify whether the location exists). If that's the case, the above method can easily be updated accordingly.

Jared Goguen
  • 8,523
  • 2
  • 14
  • 35
0

Here is a similar question with the answer I would recommend:

Elegant way to check if a nested key exists in a python dict

Alexander
  • 6,726
  • 3
  • 47
  • 62