Python Practice › Dictionary Programs › Program 18
Does a key exist?
Write a Python program to check whether a key exists in a dictionary.
Input
{"city": "pune", "state": "maharashtra"}, key "country"Output
country does not exist city exists
Approach
inchecks keys.
Solution
d = {"city": "pune", "state": "maharashtra"}for key in ["country", "city"]: print(key, "exists" if key in d else "does not exist")Output
country does not exist city exists
Learn the concept
New to this topic? Read the lesson, then come back.