To check if a Python Dictionary contains a specific key or not you can make use of the if statement with a in keyword,
Example:
# Python Dictionary containing
# names of cities
def check_for_key(cities,city_name):
if city_name in cities:
print("City {} is available in dictionary cities..".format(city_name))
else:
print("City {} is not available in dictionary cities..".format(city_name))
cities = {'Tokyo','Paris','New York', 'Mumbai', 'London'}
check_for_key(cities,'London')
check_for_key(cities,'Austin')
Output:
City London is available in dictionary cities..
City Austin is not available in dictionary cities..

-
Have Questions? Post them here!
More Posts related to Python,
- Check version of pip package installer for Python
- How to install Python 3.9 using brew on Mac
- Python Program To Calculate Simple Interest (SimpleInterest.py)
- Indent Python code in Notepad++
- Change label (text) color in tkinter
- Float built-in function in Python
- [Fix] ValueError: substring not found in Python
- pip get list of all outdated Python packages
- Calculate discount amount python code
- 3 Python program to add two numbers
- How to List all Packages installed using pip [Python]
- Python range() function examples
- How to resolve Failed to create interpreter PyCharm Error
- Format Python Code in Visual Studio Code (VS Code)
- Validate email address in Python using regular expression (regex)
- Install and Run Jupyter Notebook on Mac (macOS)
- How to delete a file using Python code example
- Base64 Encoding Decoding in Python Programming
- How to pip install Python Modules in VSCode
- Change the background of Tkinter label or text
- Comments in Python Programming
- Take input argument from command line in Python Programming
- How to Convert Python String to DateTime Object
- Tkinter - add x and y padding to label text
- [Tutorial] Install Python on Visual Studio Code (VS Code)
More Posts:
- Find your macOS version - MacOS
- SharePoint formula - Calculated columns cannot contain volatile functions like Today and Me - SharePoint
- Get Client IP address from HTTP Response in Java - Java
- BSNL Broadband upgrades speed to minimum 2MBps for all users 512Kbps 1Mbps - HowTos
- How to make div or text in html unselectable using CSS - CSS
- Installing Android Studio Dolphin on Mac with Apple (M1/M2) Chip - Android-Studio
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available - Java
- wget Command on macOS Terminal - MacOS