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 extend()
Python Set symmetric_difference()
Python Closures
Python Numbers, Type Conversion and Mathematics
Python Data Structures and Algorithms - Benjamin Baka
Python slice()
Python list()
Python String isupper()
Python String islower()
Python String splitlines()
Python List Comprehension
Python int()
Python Program to Create Pyramid Patterns
Python issubclass()
Python String rsplit()
Python Set difference_update()
Python Set issuperset()
Python Program to Add Two Matrices
Python Program to Get the Full Path of the Current Working Directory
Python Program to Slice Lists
Python print()
Python Program to Measure the Elapsed Time in Python
Python oct()
Python String ljust()
Python String replace()
Python Operator Overloading
Python float()
Python String istitle()
Python Program to Extract Extension From the File Name
Python complex()
Python String format()
Python Program to Catch Multiple Exceptions in One Line