Home > front end >  checking if each value is present in a dictionary [closed]
checking if each value is present in a dictionary [closed]

Time:10-05

So I am trying to check if each value is present in a dictionary and print "Present" if it is present and else print "Not present". The following is my code:

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

for value in my_dict.values():
    if value in my_dict:
        print("Present")
        
    else:
        print("Not present")

Every time I run this code, I am getting "Not present" as the output although all the values ("a", "b", and "c") are present in the dictionary. What is wrong with my code?

CodePudding user response:

This worked with me, so hopefully it should work with you :)

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

for value in my_dict.values():
    if value in my_dict.values():
        print("Present") 
    else:
        print("Not present")
  • Related