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 capitalize()
Python Program to Make a Flattened List from Nested List
Python if...else Statement
Python object()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python String rindex()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
How to get current date and time in Python?
Python Program to Append to a File
Python sleep()
Python len()
Python print()
Python Set clear()
Python String title()
Python Set symmetric_difference_update()
Python String upper()
Python bool()
Python String islower()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python str()
Python Data Types
Python next()
Python compile()
Python Program to Split a List Into Evenly Sized Chunks
Python String endswith()
Python dict()
Python setattr()
Python iter()
Python Program to Multiply Two Matrices
Python eval()
Python super()
Python String rpartition()