Python / How to check if multiple keys are present in dictionary

Python 3 has introduced a set literal syntax which has been backported to Python 2.7, so these days the above can be written:

>>> d = {'a': 1, 'b': 2, 'c': 3} 
>>> {'a', 'b'}.issubset(d) 
True 
>>> {'a', 'x'}.issubset(d) 
False

No comments:

Post a Comment