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 set()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Set difference_update()
Python Object Oriented Programming
Python Machine Learning - Sebastian Raschka
Python Program to Calculate the Area of a Triangle
Python String strip()
Python __import__()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Program to Get a Substring of a String
Python Dictionary pop()
Python pow()
Python Tuple
Python abs()
Python Program to Generate a Random Number
Python String rindex()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python String center()
Python Tuple count()
Python Get Current time
Python Program to Measure the Elapsed Time in Python
Python String translate()
Python while Loop
Python sorted()
Python List
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Program to Sort a Dictionary by Value
Python hex()
Python Deep Learning Cookbook - Indra den Bakker
Python exec()
Python Program to Check If Two Strings are Anagram
Python range()