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:
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Program to Get File Creation and Modification Date
Python String endswith()
Python Set intersection_update()
Python Set add()
Python compile()
Python Set difference()
Python Program to Print Colored Text to the Terminal
Python Program to Check if a Number is Positive, Negative or 0
Python String upper()
Python Program to Parse a String to a Float or Int
Introduction to Scientific Programming with Python - Joakim Sundnes
Python String center()
Python Program to Count the Occurrence of an Item in a List
Python String islower()
Python Program to Display the multiplication Table
Python Program to Capitalize the First Character of a String
Python open()
Python break and continue
Python Dictionary popitem()
Python String expandtabs()
Python print()
Python Set discard()
Python for Loop
Python Program to Calculate the Area of a Triangle
Python Iterators
Python time Module
Deep Learning with Python - Francois Cholletf
Python Type Conversion and Type Casting
Python Program to Convert Bytes to a String
Python @property decorator
Python List reverse()