Check if Key Exists in Dictionary Python Using Keys()

(Last Updated On: 07/02/2023)

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

# Check if a Key is Already Present in a Dictionary Python Using the Inbuilt method keys()

def checkKey(dic, key):
    if key in dic.keys():
        print("Present, ", end =" ")
        print("value =", dic[key])
    else:
        print("Not present")
         
# Driver Code
dic = {'a': 100, 'b':200, 'c':300}
key = 'b'
checkKey(dic, key)
 
key = 'z'
checkKey(dic, key)

Output:

Present

Leave a Reply

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