Check if a given key exists in a Python dictionary
This post will discuss how to check if a given key exists in a Python dictionary.
1. Using in operator
The preferred, the fastest way to check for the existence of a key in a dictionary is with the in operator. It returns True if the key is in the dictionary, False otherwise.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
if __name__ == '__main__': dictionary = {'A': 1, 'B': 2, 'C': 3} key = 'B' if key in dictionary: print("Key", key, "exists in the dictionary") else: print("Key doesn't exist in the dictionary") ''' Output: Key B exists in the dictionary ''' |
The in operator implement the built-in function __contains__. However, it is bad practice to call __contains__ directly. The in operator is a Pythonic way to invoke the __contains__ function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
if __name__ == '__main__': dictionary = {'A': 1, 'B': 2, 'C': 3} key = 'B' value = dictionary.__contains__(key) if value: print("Key", key, "exists in the dictionary") else: print("Key doesn't exist in the dictionary") ''' Output: Key B exists in the dictionary ''' |
2. Using dict.get() function
Another common approach to search for a key in a dictionary is using the get() function. This returns the key’s value if found; otherwise, the specified value or default value None is returned.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
if __name__ == '__main__': dictionary = {'A': 1, 'B': 2, 'C': 3} key = 'B' value = dictionary.get(key) if value: print("Key", key, "exists in the dictionary") else: print("Key doesn't exist in the dictionary") ''' Output: Key B exists in the dictionary ''' |
3. Using try/except block
You can also use the d[key] syntax to check for a key in a dictionary. This raises a KeyError when the specified key doesn’t exist in the dictionary. To handle it, use try/except block, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
if __name__ == '__main__': dictionary = {'A': 1, 'B': 2, 'C': 3} key = 'B' try: value = dictionary[key] print("Key", key, "exists in the dictionary") except KeyError: print("Key doesn't exist in the dictionary") ''' Output: Key B exists in the dictionary ''' |
4. Using setdefault() function
Finally, you can use the built-in function setdefault(), which inserts the key only if it is not found in the dictionary. This is particularly useful when you need to insert the key only if it doesn’t already exist in the dictionary.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
if __name__ == '__main__': dictionary = {'A': 1, 'B': 2, 'C': 3} key = 'B' value = dictionary.setdefault(key) if value: print("Key", key, "exists in the dictionary") else: print("Key doesn't exist in the dictionary") ''' Output: Key B exists in the dictionary ''' |
That’s all about determining whether a given key exists in a Python dictionary.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)