This question already has an answer here:
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
This question already has an answer here:
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
Use the
in
operator:Demo:
You really want to start reading the Python tutorial, the section on dictionaries covers this very subject.
Its syntax is
if key in dict:
:Now you may want to look at
collections.defaultdict
and (for the above case)collections.Counter
.