Skip to the content.

Check If A Key Exists In Dictionary

We can check if a key exists in a dictionary using a simple if statement.

Example

>>> data = {"name": "John", "age": 43}
>>> data
{'name': 'John', 'age': 43}
>>> if "name" in data:
...     print("Key present")
... else:
...     print("Not present")
...
Key present
>>> if "phone" in data:
...     print("Exists!")
... else:
...     print("Not found!")
...
Not present

Source: GeeksForGeeks