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 find()
Python dir()
Python Operators
Python Program to Create a Countdown Timer
Python dict()
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Dictionary fromkeys()
Python String split()
Python time Module
Python memoryview()
Python String isidentifier()
Python abs()
Python compile()
Python Set intersection()
Python String encode()
Python Dictionary keys()
Python Set union()
Python Dictionary copy()
Python String rjust()
Python Program to Check the File Size
Python reversed()
Python Program to Display Calendar
How to Get Started With Python?
Python bytes()
Python Program to Print Colored Text to the Terminal
Python Dictionary pop()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python locals()
Python Program to Generate a Random Number
Python Program to Calculate the Area of a Triangle
Python Set difference()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty