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 Compute all the Permutation of the String
Python pass statement
Python ascii()
Python String swapcase()
Python memoryview()
Python String join()
Python Program to Convert Bytes to a String
Python Program to Find the Size (Resolution) of a Image
Python Program to Check if a Number is Odd or Even
Python Program to Return Multiple Values From a Function
Python List sort()
Python Program to Find Armstrong Number in an Interval
Python Program to Find HCF or GCD
Python dict()
Python Program to Find the Factorial of a Number
Python Program to Sort Words in Alphabetic Order
Python List clear()
Python Data Types
Python open()
Python Dictionary copy()
Python Program to Convert Decimal to Binary Using Recursion
Python bytes()
Python len()
Python Deep Learning Cookbook - Indra den Bakker
Python for Loop
Python Program to Represent enum
Python Anonymous / Lambda Function
Python String strip()
Python frozenset()
Python locals()
Python time Module
Python Set copy()