1

I need to know if a dict is included into an other one, recursively, in Python 3:

first  = {"one":"un", "two":"deux", "three":"trois" , "sub": { "s1": "sone" }}
second = {"one":"un", "two":"deux", "three":"trois", "foo":"bar", "sub": { "s1": "sone", "s2": "stwo"}}

Usage of dictionary views like described in Test if dict contained in dict is a very nice way, but does not handle recursion case.

I came up with this function:

def isIn(inside, outside):
    for k, v in inside.items():
        try:
            if isinstance(v,dict):
                if not isIn(v, outside[k]):
                    return False
            else:
                if v != outside[k]:
                    return False
        except KeyError:
            return False

    return True

Which work:

>>> first.items() <= second.items()
False
>>> isIn(first, second)
True

Is there a better (more Pythonic) way?

karel
  • 5,489
  • 46
  • 45
  • 50
PierreL
  • 63
  • 4
  • If the first argument is empty, any second argument will pass the test. For example: `isIn({}, 9999) == True`. – FMc May 03 '16 at 13:29

1 Answers1

2

Here's a bit shorter version which doesn't need try/except and handles the case where the parameters are different type:

def isIn(inside, outside):
    if isinstance(inside, dict) and isinstance(outside, dict):
        return all(isIn(v, outside.get(k, object())) for k, v in inside.items())
    return inside == outside

print(isIn(first, second)) # True
print(isIn(second, first)) # False
print(isIn({}, 9999)) # False
niemmi
  • 17,113
  • 7
  • 35
  • 42