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 Objects and Classes
Python Program to Check If a List is Empty
Python timestamp to datetime and vice-versa
Python Program to Count the Number of Occurrence of a Character in String
Python Program to Convert String to Datetime
Python Program to Sort Words in Alphabetic Order
Python Program to Convert Bytes to a String
Python Dictionary values()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python dir()
Python List count()
Python chr()
Python Program to Create a Long Multiline String
Python Operator Overloading
Python Functions
Python map()
Python hasattr()
Python Program to Find the Sum of Natural Numbers
Python Errors and Built-in Exceptions
Python Program to Get the Class Name of an Instance
Python Strings
Python Program to Find the Size (Resolution) of a Image
Python Program to Get File Creation and Modification Date
Python Set isdisjoint()
Python Global Keyword
Python String rpartition()
Python set()
Python Program to Find the Factorial of a Number
Python List remove()
Python Program to Find Factorial of Number Using Recursion
Deep Learning in Python - LazyProgrammer