Find if a key exist in python dictionary
·91 words·1 min
Hi today i’m going to show you how to find if a key exist in a python dictionary.
Plan
-
Make a dictionary if you don’t know look at my mini lesson about dictionary.
-
Make a variable called key and get the key
-
Use if statement if key then your dictionary name: and print(True) else print(False) the reason why you don’t use str is cuz it’s a boolean
Your done.
colour_dict1 = {"banana": "yellow", "blueberry": "blue", "strawberry": "pink" }
colour_dict = {}
key = "banana"
if key in colour_dict1:
print(True)
else:
print(False)