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 *args and **kwargs
Python hasattr()
Python List extend()
Python Program to Catch Multiple Exceptions in One Line
Python break and continue
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Keywords and Identifiers
Python List sort()
Python Program to Check If a List is Empty
Debug a JavaMail Program
Python hash()
Python Program to Calculate the Area of a Triangle
Python String center()
Python Program to Get the Last Element of the List
Python Program to Randomly Select an Element From the List
Python List remove()
Python property()
Python next()
Python Program to Split a List Into Evenly Sized Chunks
Python Program to Find Numbers Divisible by Another Number
Python Set difference_update()
Python Set update()
Python Program to Display the multiplication Table
Python String isdecimal()
Python Program to Check If a String Is a Number (Float)
Python Program to Reverse a Number
Python super()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Recursion
Python globals()
Python Program to Check Prime Number
Intelligent Projects Using Python - Santanu Pattanayak