Check if Key Exists in Dictionary Python

(Last Updated On: 03/02/2023)

Python Program to Check if Key Exists in Dictionary Using in Keyword

#Write a Python Program to Check if a Key is Already Present in a Dictionary Using in keyword

my_dict = {1: 'a', 2: 'b', 3: 'c', 4: 'd'}

# Using if statement and in keyword, you can check if a key is present in a dictionary
if 4 in my_dict:
    print("Present")

Output:

Present

Leave a Reply

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