How to Check If a Specified Key exists in a Dictionary in Python


In this example we will show how to determine if a specified key is present in a dictionary in Python.

Source Code

mydict = {
  'name': 'Steven',
  'age': 24,
  'ID': 123456
}

if 'name' in mydict:
  print("'name' is one of the keys in the thisdict dictionary.")
else:
    print("'name' is not one of the keys in the thisdict dictionary.")

Output:

'name' is one of the keys in the thisdict dictionary.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments