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 Tuple index()
Python Program to Append to a File
Python bin()
Python int()
Python Inheritance
Python Recursion
Python Set issuperset()
Python Functions
Python frozenset()
Python Set intersection()
Python iter()
Python *args and **kwargs
Python String maketrans()
Python String format_map()
Python Numbers, Type Conversion and Mathematics
Intelligent Projects Using Python - Santanu Pattanayak
Python dict()
Python type()
Python Program to Swap Two Variables
Python Program to Create a Countdown Timer
Python len()
Python Program to Find the Largest Among Three Numbers
Python Program to Sort Words in Alphabetic Order
Python round()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python str()
Python String rindex()
Python Set pop()
Python oct()
Python Program to Reverse a Number
Python sleep()
Python Function Arguments