In this python dictionary program, we will check whether a key exists in the dictionary or not.
Steps to solve the program
- Take a dictionary as input.
- Using an if-else statement check whether the given keys exist in the dictionary.
- If yes then the print key exists in the dictionary.
- If False then the print key does not exist.
D1 = {'city':'pune','state':'maharashtra'}
count = 0
for key in D1.keys():
if key == "Country":
count += 1
if count > 0:
print("Key exists")
else:
print("Key does not exists")
Output :
Key does not exists