Because the grouping of expressions would be like -
if ('verision') and ('importance' in a):
And 'version'
string is True
in boolean context (Only empty string is False
in boolean context, all other non-empty strings are True, read more about it here). , so it short circuits the and
condition and returns True
. You want -
if 'verision' in a and 'importance' in a:
For only 2 keys i would suggest the above version , but if you have more than 2 keys , you can create a set of those keys as suggested in the comments by @Duncan and check if that set is a subset of the dictionary keys. Example -
needs = set(['version','importance','otherkeys'..]) #set of keys to check
if needs.issubset(a): #This can be written as a single line, but that would make the line very long and unreadable.
Demo -
>>> a={u'is_l': 0, u'importance': 1, u'notes': u'12345', u'created_by': 1 }
>>> needs=set(['verision','importance'])
>>> needs.issubset(a)
False