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..

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!