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 String replace()
Python frozenset()
Python List
Python Program to Find Hash of File
Python Function Arguments
Python filter()
Python super()
Python dict()
Python compile()
Python Program to Find LCM
Python Program to Find Sum of Natural Numbers Using Recursion
Python time Module
Python Type Conversion and Type Casting
Python Machine Learning - Sebastian Raschka
Python Program to Display Calendar
Python Program to Sort a Dictionary by Value
Python Modules
Python String zfill()
Python *args and **kwargs
Python Program to Convert String to Datetime
Python Program to Split a List Into Evenly Sized Chunks
Python open()
Python Set remove()
Python String rindex()
How to get current date and time in Python?
Python hex()
Python Exception Handling Using try, except and finally statement
Python String lstrip()
Python String istitle()
Python Program to Print Output Without a Newline
Python frozenset()
Python String center()