How to check that the key is defined in dictionary in python?
a={} ... if 'a contains key b':   a[b] = a[b]+1 else   a[b]=1 
                Python dictionary doesn't allow key to be repeated.
Check if two nested dictionaries are equal in Python To do this task, we are going to use the == operator and this method will help the user to check whether the two given dictionaries are equal or not.
Use the in operator:
if b in a:   Demo:
>>> a = {'foo': 1, 'bar': 2} >>> 'foo' in a True >>> 'spam' in a False   You really want to start reading the Python tutorial, the section on dictionaries covers this very subject.
Its syntax is if key in dict: : 
if "b" in a:     a["b"] += 1 else:     a["b"] = 1   Now you may want to look at collections.defaultdict and (for the above case) collections.Counter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With