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

To understand this example, you should have the knowledge of the following Python programming topics:


Using in keyword

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

if 2 in my_dict:
    print("present")

Output

present

Using if statement and in keyword, you can check if a key is present in a dictionary.

In the above example, 2 is present in the dictionary as a key; therefore, the output is present.

You can use not in if you want to check if a key is not present in the dictionary.


Also Read:

Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community