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
1 2 3 4 | my_dict = { 1 : 'a' , 2 : 'b' , 3 : 'c' } if 2 in my_dict: print ( "present" ) |
Output
1 | 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 Dictionary update()
Python Program to Generate a Random Number
Python if...else Statement
Python Set symmetric_difference()
Python String rfind()
Python Program to Differentiate Between del, remove, and pop on a List
Python Dictionary pop()
Python String isdecimal()
Python Set difference()
Python String rindex()
Python Dictionary fromkeys()
Python Data Structures and Algorithms - Benjamin Baka
Python String istitle()
Python Program to Append to a File
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python String rsplit()
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Program to Find Hash of File
Python Set pop()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python help()
Python Program to Get the Class Name of an Instance
Python Set symmetric_difference_update()
Python Program to Display Calendar
Python Program to Illustrate Different Set Operations
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Statement, Indentation and Comments
Python classmethod()
Python min()
Python Program to Remove Duplicate Element From a List
Python Program to Convert Bytes to a String
Python ascii()