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 add()
Python Program to Get File Creation and Modification Date
Python Statement, Indentation and Comments
Python bytes()
Python Set copy()
Python list()
Python slice()
Python bin()
Python Program to Convert Bytes to a String
Python Dictionary get()
Python Program to Print the Fibonacci sequence
Python Program to Delete an Element From a Dictionary
Python callable()
Python hex()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python String splitlines()
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Deep Learning Cookbook - Indra den Bakker
Python Operators
Python Generators
Python chr()
Python filter()
Python map()
Python String istitle()
Python *args and **kwargs
Python memoryview()
Python Set union()
Python String rpartition()
Python setattr()
Python Program to Print Output Without a Newline
Java Program to Implement the Program Used in grep/egrep/fgrep
Python Matrices and NumPy Arrays