In this example, you will learn to check if a key is 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.
Related posts:
Python Set difference_update()
Python String find()
Python Program to Iterate Over Dictionaries Using for Loop
Python Program to Find HCF or GCD
Python Program to Find LCM
Python Program to Generate a Random Number
Python Program to Compute the Power of a Number
Python List count()
Python String swapcase()
Python Program to Find the Square Root
Python String splitlines()
Python Set issuperset()
Python vars()
Python property()
Python Program to Shuffle Deck of Cards
Python Custom Exceptions
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Program to Merge Two Dictionaries
Python File I/O Operation
Python Program to Multiply Two Matrices
Python String isalpha()
Python List remove()
Python Program to Add Two Numbers
Python Anonymous / Lambda Function
Python String zfill()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Dictionary setdefault()
Python Machine Learning - Sebastian Raschka
Python String count()
Python Errors and Built-in Exceptions
Python Program to Create Pyramid Patterns
Python Program to Check If a String Is a Number (Float)