Check whether key exist in Python dictionary or not?

This Python example explains how to check whether certain key exist or not in Python dictionary.

Python Source Code: Chek Existence of Key in Python Dictionary


dct = {'first':1, 'second':2, 'third':4}

if 'first' in dct:
    print('Key exist!')
else:
    print('Key does not exist!')
    
if 'second' in dct:
    print('Key exist!')
else:
    print('Key does not exist!')
    
if 'third' in dct:
    print('Key exist!')
else:
    print('Key does not exist!')

if 'fourth' in dct:
    print('Key exist!')
else:
    print('Key does not exist!')   

Output

Key exist!
Key exist!
Key exist!
Key does not exist!