.get .
if my_dict.get('some_key'):
# Do something
There is one caveat: if the key exists, but is false, then it will not pass the test, which may not be what you want. Keep in mind that this is rarely the case. Now the reverse problem is more frequent. This is used into check for a key. I often found this problem when reading csv files.
Example
a,b
1,1
1,
import csv
with open('path/to/file', 'r') as fh:
reader = csv.DictReader(fh)
for row_d in reader:
if 'b' in row_d:
source
share