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 Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Parse a String to a Float or Int
Python strptime()
Python String endswith()
Python Program to Trim Whitespace From a String
Python List append()
Python Program to Split a List Into Evenly Sized Chunks
Python Errors and Built-in Exceptions
Python Shallow Copy and Deep Copy
Python Program to Capitalize the First Character of a String
Python Set intersection_update()
Python Program to Print all Prime Numbers in an Interval
Python if...else Statement
Python String rfind()
Python String index()
Python id()
Python Set difference_update()
Python dict()
Python open()
Python float()
Python Program to Merge Two Dictionaries
Python Program to Check if a Number is Positive, Negative or 0
Python Dictionary setdefault()
Python Program to Print the Fibonacci sequence
Python Program to Generate a Random Number
Python Program to Find Numbers Divisible by Another Number
Python Program to Append to a File
Python repr()
Python String split()
Python Program to Convert Two Lists Into a Dictionary
Python bytearray()
Python Program to Get the Full Path of the Current Working Directory