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 open()
Python locals()
Python Program to Trim Whitespace From a String
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python Program to Check If Two Strings are Anagram
Python staticmethod()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python Program to Check Armstrong Number
Python memoryview()
Python Deep Learning Cookbook - Indra den Bakker
Python Functions
Python Program to Capitalize the First Character of a String
Python Data Types
Python for Loop
Python property()
Python Sets
Python Input, Output and Import
Python List clear()
Python String startswith()
Python Dictionary values()
Python String replace()
APIs in Node.js vs Python - A Comparison
Python hasattr()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Program to Print Output Without a Newline
Python vars()
Python Dictionary items()
Python String lstrip()
Python String expandtabs()
Python Program to Make a Simple Calculator
Python Program to Find the Sum of Natural Numbers
Deep Learning in Python - LazyProgrammer