Check whether a key exists in the dictionary or not.

In this python dictionary program, we will check whether a key exists in the dictionary or not.

Steps to solve the program
  1. Take a dictionary as input.
  2. Using an if-else statement check whether the given keys exist in the dictionary.
  3. If yes then the print key exists in the dictionary.
  4. 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
				
			

get the size of a dictionary in python.

iterate over a dictionary.

Leave a Comment