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 List count()
Python List clear()
Python List Comprehension
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
How to Get Started With Python?
Python datetime
Python Set intersection()
Python Program to Print Output Without a Newline
Python Program to Reverse a Number
Python reversed()
Python break and continue
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Compute the Power of a Number
Python Program to Display Fibonacci Sequence Using Recursion
Python String rpartition()
Python Dictionary copy()
Python Program to Count the Occurrence of an Item in a List
Python String lower()
Python String rindex()
Python Program to Check If a List is Empty
Python Input, Output and Import
Python pow()
Python String capitalize()
Python *args and **kwargs
Python repr()
Python Program to Find the Sum of Natural Numbers
Python RegEx
Python Program to Generate a Random Number
Python pass statement
Python bool()
Python frozenset()
Python Program to Find Armstrong Number in an Interval