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 Program to Randomly Select an Element From the List
Python Program to Differentiate Between del, remove, and pop on a List
Python Program to Sort a Dictionary by Value
Python String lstrip()
Python Program to Find HCF or GCD
Python Program to Get the Full Path of the Current Working Directory
Python String join()
Python List pop()
Deep Learning in Python - LazyProgrammer
Python chr()
Python Statement, Indentation and Comments
Python String format_map()
Python String title()
Python Machine Learning - Sebastian Raschka
Python Matrices and NumPy Arrays
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Program to Compute the Power of a Number
Python Program to Count the Number of Each Vowel
Python Tuple index()
Python Dictionary popitem()
Python Program to Create a Countdown Timer
Python String isdigit()
Python String rjust()
Python strftime()
Python Program to Multiply Two Matrices
Python Program to Convert Kilometers to Miles
Python bytes()
Python id()
Python if...else Statement
Python Program to Print all Prime Numbers in an Interval
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Set issubset()