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

Machine Learning Projects

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

Leave a Comment

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