How to check if a key exists in a Python dictionary?

PythonServer Side ProgrammingProgramming

You can check if a key exists in a Python dictionary using the in operator. The in operator matches the key against dictionary and checks for the presence of key. 

example

my_dict = {'name': 'TutorialsPoint', 'time': '15 years', 'location': 'India'}
print('name' in my_dict)
print('foo' in my_dict)

Output

This will give the output −

True
False
raja
Published on 05-Jan-2018 17:35:49
Advertisements