Check key exist or not in dictionary in python

To check Key exist or not in Python, you can useinoperator.

user = {
            'name':'Sheetal',
            'company': 'Impulsive Web',
            'role':'Tech Lead'
       }

if 'role' in user:
    print('Exist')
else:
    print('Not Exist')

Output:

Exist

 

You can also use:

dict.has_key(key) to check key exist. It is depricated.


Keywords: