Check if Key Exists in Dictionary Python Using Count()

(Last Updated On: 07/02/2023)

Python Program to Check if Key Exists in Dictionary Using Count()

# Check if a Key is Already Present in a Dictionary Python using count()

# Python 3 Program to check whether the given key already exists in a dictionary.

# Driver Code
dic = {'a': 100, 'b': 200, 'c': 300}
key = 'b'
x = list(dic.keys())
res = "Not Present"
if(x.count(key) == 1):
	res = "Present"
print(res)

Output:

Present

Leave a Reply

Your email address will not be published. Required fields are marked *