Is there a smart pythonic way to check if there is an item (key,value) in a dict?
a={\'a\':1,\'b\':2,\'c\':3}
b={\'a\':1}
c={\'a\':2}
b in a:
--> True
c
>>> a = {'a': 1, 'b': 2, 'c': 3}
>>> b = {'a': 1}
>>> c = {'a': 2}
First here is a way that works for Python2 and Python3
>>> all(k in a and a[k] == b[k] for k in b)
True
>>> all(k in a and a[k] == c[k] for k in c)
False
In Python3 you can also use
>>> b.items() <= a.items()
True
>>> c.items() <= a.items()
False
For Python2, the equivalent is
>>> b.viewitems() <= a.viewitems()
True
>>> c.viewitems() <= a.viewitems()
False