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:
Deep Learning with Python - Francois Cholletf
Python Program to Concatenate Two Lists
Python Program to Count the Number of Digits Present In a Number
Python Machine Learning Eqution Reference - Sebastian Raschka
Python List remove()
Python __import__()
Python String rjust()
Python Program to Check if a Number is Positive, Negative or 0
Python Dictionary fromkeys()
Python List reverse()
Python Closures
Python Program to Check if a Number is Odd or Even
Python Program to Find the Factors of a Number
Deep Learning in Python - LazyProgrammer
Python Program to Count the Occurrence of an Item in a List
Python Package
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Program to Check Armstrong Number
Python Dictionary keys()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Program to Iterate Over Dictionaries Using for Loop
Python String swapcase()
Python Program to Illustrate Different Set Operations
Python Set update()
Python Program to Create a Countdown Timer
Python Program to Create a Long Multiline String
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Program to Check Leap Year
Python String rsplit()
Python Function Arguments
Python str()
Python Program to Find HCF or GCD