Python Program to Check if a Key is Already Present in a Dictionary – 2024

Hey guys, in this blog we will see 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'}

if 2 in my_dict:
    print("present")

Output

present

  • Here we are simply checking if key 2 is present in our dictionary or not.
  • Note we can check for keys using the following also:
my_dict = {1: 'a', 2: 'b', 3: 'c'}

if 2 in my_dict.keys():
    print("present")

Check out our other python programming examples

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 514

Leave a Reply

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