In this article we will discuss how to check if a key exists in dictionary.
Suppose we have a dictionary of string and int i.e.
wordFreDic = {
'Hello':56,
'at':23,
'test':43,
'this':78,
}
Now let’s check if key ‘test’ is present in dictionary or not.
Check if key exists in dictionary using in operator
# Check if dict contains any entry with key 'test'
if "test" in wordFreqDic:
print("Yes 'test' key exists in dict")
else:
print("No 'test' key does not exists in dict")
output
Yes 'test' key exists in dict
check if key exists in dictionary using get()
Dictionary provides a method get() that accepts a key and default value.
dict.get(key[, default])
If given key exists in dictionary then it returns its value,
If given key does not exists in dictionary then it returns the passed default value argument.
If given key does not exists in dictionary and Default value is also not passed in get() function, then get() function will return None.
Let’s use get() this to check if given key exists in dictionary,
'''
Check if key exists in dictionary using get()
'''
if wordFreqDic.get("test") != None:
print("Yes 'test' key exists in dict")
else:
print("No 'test' key does not exists in dict")
As ‘test’ key exists in the dictionary and its value is not None, so output is,
Yes 'test' key exists in dict
But what if we are call dict.get() with key that exists in the dictionary with value ‘None’ i.e.
Adding key “from” in dict with value None
wordFreqDic["from"] = None
Now wordFreqDic.get(“from”) will return None.
So, we can not be always sure with the result of dict.get() that key exists in dictionary. Therefore, we should use dict.get() to check existence of key in dictionary only in scenarios when we are sure that there can not be an entry of key with given default value.
For example, above dictionary contains occurrence count of given words in an article, so value of keys should always be 0 or more.
So in this scenario, to check if the given key exists in dictionary we should pass -1 as default value in dict.get() i.e.
if wordFreqDic.get("from", -1) != -1:
print("Yes 'from' key exists in dict")
else:
print("No 'from' key does not exists in dict")
output
Yes 'from' key exists in dict
Complete example is as follows,
def main():
# Dictionary of string and int
wordFreqDic = {
"Hello": 56,
"at" : 23 ,
"test" : 43,
"this" : 78
}
print(wordFreqDic)
'''
Check if key exists in dictionary using 'in' operator
'''
# Check if dict contains any entry with key 'test'
if "test" in wordFreqDic:
print("Yes 'test' key exists in dict")
else:
print("No 'test' key does not exists in dict")
'''
Check if key exists in dictionary using get()
'''
if wordFreqDic.get("test") != None:
print("Yes 'test' key exists in dict")
else:
print("No 'test' key does not exists in dict")
# But what of any element in dictionary has value None i.e.
wordFreqDic["from"] = None
print(wordFreqDic)
if wordFreqDic.get("from", -1) != -1:
print("Yes 'from' key exists in dict")
else:
print("No 'from' key does not exists in dict")
if __name__ == '__main__':
main()
output
{'at': 23, 'this': 78, 'test': 43, 'Hello': 56}
Yes 'test' key exists in dict
Yes 'test' key exists in dict
{'from': None, 'at': 23, 'this': 78, 'test': 43, 'Hello': 56}
Yes 'from' key exists in dict