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 Machine Learning Eqution Reference - Sebastian Raschka
Python Program to Compute all the Permutation of the String
Python String split()
Python Data Structures and Algorithms - Benjamin Baka
Python while Loop
Python String title()
Python filter()
Python complex()
Python exec()
Python Keywords and Identifiers
Python String rjust()
Python Set intersection()
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Program to Differentiate Between del, remove, and pop on a List
Python Program to Get File Creation and Modification Date
Python Inheritance
Python Program to Print all Prime Numbers in an Interval
Python Dictionary items()
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Set difference_update()
Python Program to Add Two Numbers
Python List Comprehension
Python open()
Python Set update()
Python Program to Remove Duplicate Element From a List
Python Program to Catch Multiple Exceptions in One Line
Python String swapcase()
Python Program to Find the Factors of a Number
Python String isupper()
Python List insert()
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python pass statement